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


我正试图将图标置于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>

我在使用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);

8395
2018-01-30 03:15


起源

当您从父亲relativelayout中删除android:orientation =“vertical”时,您能找到任何差异吗?我建议你不要在relativelayout(第二个)中再次包装imageview并检查。 - Kanth
刚尝试在图像视图周围没有相对布局,并且从主布局中删除了android:orientation。图像最终位于按钮顶部。按钮正是我想要的位置,我只需将该图像置于操作栏中。 - smokingoyster
看到我的回答。如果你还没有得到,请告诉我。 - Kanth


答案:


好的。这应该工作。尝试这个(经过正常活动测试):

<?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>

8
2018-01-30 04:07



这是将imageview放在按钮的顶部。当您在正常活动中对此进行测试时,它是该活动的操作栏吗? - smokingoyster
这实际上有效,但前提是你没有在OnCreateOptionsMenu中膨胀菜单。因此,如果您需要右手按钮,则必须明确添加它们。 - smokingoyster


之前的答案对我不起作用(在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);

6
2018-01-07 09:50



这看起来像是对我的黑客攻击。更好的解决方案? - Ra1nWarden
这很聪明,我喜欢它 - Bibaswann Bandyopadhyay


您可以将自定义视图添加到操作栏。只需添加一个LinearLayout作为自定义视图,并将子视图添加到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>

1
2018-01-30 03:25



我已经在使用自定义视图了。问题中的布局是自定义视图的布局。我已编辑显示android代码。 - smokingoyster
此布局使按钮和图像视图直接相邻。我想知道它是否是一个动作栏的事实是问题。 - smokingoyster
你在eclipse布局编辑器中尝试过这种布局吗?我从头开始生成这个,而不是使用你的东西。您需要将我在布局中放置的按钮替换为您需要的任何其他视图。 - Nathan
我修改它以使用我的按钮/图像。 imageview直接位于图像按钮旁边。不在动作栏的中心。我想知道操作栏如何管理其视图有些奇怪,因为它正在添加右侧的菜单项。 - smokingoyster