【问题标题】:Center image in action bar AndroidAndroid操作栏中的中心图像
【发布时间】:2015-06-03 11:50:09
【问题描述】:

我在 android 中有一个自定义操作栏,其图像必须在操作栏中居中。 “Brancom”图像必须在中间,但我无法让它工作,因为它旁边有一个汉堡包图标。我该如何解决?这是它的样子:http://gyazo.com/30eac941ea964c143012d470a2d76d3b

如您所见,它不在中心。这是我的代码:

<?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:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/actionBarLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:clickable="false"
        android:focusable="false"
        android:longClickable="false"
        android:src="@drawable/ic_brancom_actionbar" />

</LinearLayout>

这是我的java代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    final ActionBar actionBar = getActionBar();
    actionBar.setCustomView(R.layout.actionbar_custom_view_home);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);

    and then the rest of the code...

编辑

这就是正在发生的事情:http://gyazo.com/c014f55d10aa0932a9ccbaea97bae674

编辑#2

我通过添加这个来解决这个问题,显然图标是 60dp,所以我在将其设为相对布局后从中心减去它:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/actionBarLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false"
        android:longClickable="false"
        android:src="@drawable/ic_brancom_actionbar"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="60dp"
        android:layout_marginEnd="60dp" />

</RelativeLayout>

【问题讨论】:

    标签: android xml styling


    【解决方案1】:

    尝试像这样将重力赋予 ImageView 并更好地使用 RelativeLayout 而不是 linearLayout:

    android:layout_gravity="center_horizontal|center_vertical"
    

    【讨论】:

    • a RelativeLayout 只是将一个视图居中就像使用火箭筒杀死苍蝇一样。一个简单的FrameLayout 就足够了
    【解决方案2】:

    您可以将标题图像转换为 9-patch 或 XML drawable,以便它可以适应操作栏的大小,并将其用作操作栏背景。

    请注意,使用此解决方案时,如果您有很多可见的操作项,它们可能会超出标题图像。

    XML 可绘制示例

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item>
            <shape>
                <solid android:color="@color/actionbar_color"/>
            </shape>
        </item>
    
        <item>
            <bitmap
                android:src="@drawable/title_image"
                android:gravity="center"/>
        </item>
    
    </layer-list>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多