【问题标题】:How To Center Icon in Android Action Bar如何在 Android 操作栏中将图标居中
【发布时间】:2013-01-13 19:14:27
【问题描述】:

我正在尝试使图标在 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);

【问题讨论】:

  • 当您从父 relativelayout 中删除 android:orientation = "vertical" 时,您能发现任何不同吗?我建议你不要在 relativelayout(第二个)中再次包装 imageview 并检查。
  • 刚刚尝试在图像视图周围没有相对布局,并从主布局中删除了 android:orientation。图像最终位于按钮顶部。该按钮正是我想要的位置,我只需将该图像置于操作栏中的中心即可。
  • 看我的回答。如果您仍然不明白,请告诉我。

标签: android android-layout android-actionbar actionbarsherlock


【解决方案1】:

好的。这应该有效。试试这个(在正常活动中测试):

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

【讨论】:

  • 这是把图像视图放在按钮的顶部。当您在正常活动中测试它时,它是该活动的操作栏吗?
  • 这确实有效,但前提是您不膨胀 OnCreateOptionsMenu 中的菜单。因此,如果您想要右手按钮,则必须明确添加它们。
【解决方案2】:

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

【讨论】:

  • 这对我来说似乎是一个黑客行为。有更好的解决方案吗?
  • 好聪明,我喜欢
【解决方案3】:

您可以向操作栏添加自定义视图。只需添加一个 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>

【讨论】:

  • 我已经在使用自定义视图了。问题中的布局是自定义视图的布局。我已经编辑以显示 android 代码。
  • 此布局的按钮和图像视图直接相邻。我想知道它是一个操作栏这一事实是否是问题所在。
  • 你在 Eclipse 布局编辑器中尝试过这个布局吗?我从头开始生成这个,而不是使用你的东西。您需要将我放入布局中的按钮替换为您需要的任何其他视图。
  • 我修改它以使用我的按钮/图像。图像视图直接位于图像按钮旁边。不在操作栏的中心。我想知道操作栏管理其视图的方式是否有些奇怪,因为它在右侧添加了菜单项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
相关资源
最近更新 更多