ActionBar-帶有居中ImageView的自定義視圖,側(cè)面有操作項我需要將“首頁”活動的自定義徽標(biāo)(使用ImageView)居中在操作欄中。我正在為此項目使用ABS。這與SO上發(fā)布的另一個問題(ActionBar徽標(biāo)居中,Action項位于側(cè)面)非常相似,但是我不確定ImageView或搜索菜單是否有所作為,因為我沒有得到想要的結(jié)果(以居中的圖片為準(zhǔn)),或者如果我剛弄錯的話?;旧?,我在左側(cè)設(shè)置一個圖標(biāo),在中心插入自定義視圖,在右側(cè)添加一個搜索圖標(biāo)(OptionsMenu)。該圖像的確確實顯示在圖標(biāo)的右側(cè),但是仍然居中。任何有關(guān)如何在操作欄中居中放置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貢獻(xiàn)1829條經(jīng)驗 獲得超7個贊
代碼中的ImageView相對于LinearLayout居中,而不是相對于操作欄居中。您可以將左邊緣(android:layout_marginLeft)添加到布局中以調(diào)整圖像位置。
這樣做的另一種方法不是在操作欄中添加圖標(biāo)和操作項,而是使用帶有圖標(biāo)和按鈕的自定義布局。但是在這種情況下,您將需要自己處理操作項。
- 4 回答
- 0 關(guān)注
- 521 瀏覽
添加回答
舉報
0/150
提交
取消