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)添加到布局中以調整圖像位置。
這樣做的另一種方法不是在操作欄中添加圖標和操作項,而是使用帶有圖標和按鈕的自定義布局。但是在這種情況下,您將需要自己處理操作項。
- 4 回答
- 0 關注
- 522 瀏覽
添加回答
舉報
0/150
提交
取消