如何在Android操作栏中居中图标

smo*_*ter 11 android android-layout actionbarsherlock android-actionbar

我正试图将图标置于android操作栏中.我使用的是自定义布局,左侧有一个按钮,中间有一个图标,右侧有菜单选项.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ActionBarWrapper"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



        <ImageButton
            android:id="@+id/slideMenuButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_menu_bookmark" />

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/RelativeLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerInParent="true"  >
            <ImageView
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher"
                android:layout_centerInParent="true" />
        </RelativeLayout>



</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

无论RelativeLayout包装ImageView,我都试过这个.左侧按钮显示正常,我可以使用layout_toRightOf设置图标,但我无法将其置于中心位置.有人有主意吗?

编辑:这是on create方法中的android代码.

ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayOptions(actionBar.DISPLAY_SHOW_CUSTOM);
    View cView = getLayoutInflater().inflate(R.layout.actionbar, null);
    actionBar.setCustomView(cView);
Run Code Online (Sandbox Code Playgroud)

Kan*_*nth 8

好的.这应该工作.试试这个(在正常活动中测试):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ActionBarWrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/slideMenuButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_alignParentLeft="true" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true"  />    

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)


小智 6

之前的答案对我不起作用(在Android 4.4设备上),因为外部容器视图组没有扩展到完全,因此容器组和居中的内部徽标已在标题栏中保持对齐.

我找到了一个适用于常规操作栏菜单(右侧)的布局,无论是否启用常规操作栏标识和标题(左侧).此示例仅以额外徽标为中心.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
>

    <View android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
    />

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@drawable/my_logo"
               android:layout_gravity="center_vertical"
               android:contentDescription="Logo"
        />

    <View android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
      />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

您必须通过此方式启用自定义视图

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowCustomEnabled(true);
View cView = getLayoutInflater().inflate(R.layout.actionbar, null);
actionBar.setCustomView(cView);
Run Code Online (Sandbox Code Playgroud)

但不是这种方法(它禁用了操作栏中的所有其他元素).

// actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Run Code Online (Sandbox Code Playgroud)