12

我正在尝试将图标居中在 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>

我已经尝试过使用和不使用包装 ImageView 的 RelativeLayout。左侧按钮显示正常,我可以使用 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);
4

3 回答 3

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>
于 2013-01-30T04:07:30.450 回答
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>

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

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

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

// actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
于 2015-01-07T09:50:50.220 回答
1

您可以将自定义视图添加到操作栏。只需添加一个 LinearLayout 作为自定义视图并将子视图添加到线性布局。我使用这种技术在中间添加了 4 个按钮。

要添加自定义视图,请使用 actionBar.setCustomView(view)。还使用 actionBar.setDisplayOptions 设置选项 DISPLAY_SHOW_CUSTOM。

一旦你在栏中有一个自定义视图,然后使用正常的布局程序来获得你想要的效果。您可以使用的一个技巧是拥有 3 个嵌套的线性布局并为每个布局分配一个权重。例如 1,4,1,因此中心布局获得最大空间。这是一个示例,当然,如果您希望它为空白,则可以省略右侧布局上的按钮。使用这种方法,您可以达到精确的中心。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#F00"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#0F0"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#00F"
        android:gravity="right"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>
</LinearLayout>
于 2013-01-30T03:25:07.443 回答