我需要在“主页”活动的操作栏中将自定义徽标(使用 ImageView)居中。我在这个项目中使用 ABS。这与 SO 上发布的另一个问题( ActionBar logo centered and Action items on side)非常相似,但我不确定 ImageView 或搜索菜单是否有所不同,因为我没有得到我正在寻找的结果对于(居中的图像),或者如果我弄错了。基本上,我在左侧设置了一个图标,在中心插入自定义视图,并在右侧(OptionsMenu)有一个搜索图标。图像确实出现在图标的右侧,但它仍然居中。任何关于如何在操作栏中居中 ImageView 的指针将不胜感激。
主页.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);
}
@Override
public 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>
资源/菜单/search.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@id/search_item"
android:icon="?attr/action_search"
android:title="@string/search_label"
android:showAsAction="ifRoom|collapseActionView">
</item>
</menu>