亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ActionBar-帶有居中ImageView的自定義視圖,側面有操作項

ActionBar-帶有居中ImageView的自定義視圖,側面有操作項

繁花不似錦 2019-12-16 10:08:15
ActionBar-帶有居中ImageView的自定義視圖,側面有操作項我需要將“首頁”活動的自定義徽標(使用ImageView)居中在操作欄中。我正在為此項目使用ABS。這與SO上發布的另一個問題(ActionBar徽標居中,Action項位于側面)非常相似,但是我不確定ImageView或搜索菜單是否有所作為,因為我沒有得到想要的結果(以居中的圖片為準),或者如果我剛弄錯的話。基本上,我在左側設置一個圖標,在中心插入自定義視圖,在右側添加一個搜索圖標(OptionsMenu)。該圖像的確確實顯示在圖標的右側,但是仍然居中。任何有關如何在操作欄中居中放置ImageView的指針將不勝感激。Home.java:public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_home);    LayoutInflater inflater = (LayoutInflater) getSupportActionBar().getThemedContext()            .getSystemService(LAYOUT_INFLATER_SERVICE);    final View customActionBarView = inflater.inflate(            R.layout.actionbar_custom_view_home, null);    /* Show the custom action bar view and hide the normal Home icon and title */    final ActionBar actionBar = getSupportActionBar();    actionBar.setHomeButtonEnabled(true);    actionBar.setDisplayHomeAsUpEnabled(false);    actionBar.setDisplayShowTitleEnabled(false);    actionBar.setIcon(R.drawable.ic_ab_som);    actionBar.setCustomView(customActionBarView);    actionBar.setDisplayShowCustomEnabled(true);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    MenuInflater inflater = new MenuInflater(this);    inflater.inflate(R.menu.search, menu);    return true;}res / layout / actionbar_custom_view_home.xml:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_gravity="center"><ImageView    android:id="@+id/actionBarLogo"    android:contentDescription="@string/application_name"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:clickable="false"    android:duplicateParentState="false"    android:focusable="false"    android:longClickable="false"    android:padding="@dimen/padding_small"    android:src="@drawable/logo_horizontal" /></LinearLayout>
查看完整描述

4 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

代碼中的ImageView相對于LinearLayout居中,而不是相對于操作欄居中。您可以將左邊緣(android:layout_marginLeft)添加到布局中以調整圖像位置。

這樣做的另一種方法不是在操作欄中添加圖標和操作項,而是使用帶有圖標和按鈕的自定義布局。但是在這種情況下,您將需要自己處理操作項。


查看完整回答
反對 回復 2019-12-16
  • 4 回答
  • 0 關注
  • 522 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號