如何在android的动作栏工具栏中心放置徽标?

时间:2016-06-29 16:08:53

标签: android android-actionbar position toolbar

我想在Action Bar的中心显示徽标。这是我的布局代码:下面的代码应该可以工作,但它不是为什么?请帮帮我

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:layout_gravity="center"
        />

</LinearLayout>

在onCreate()

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.custom_logo);

1 个答案:

答案 0 :(得分:2)

您的工具栏就像一个可以容纳其他视图的ViewGroup。所以更简单的方法是将ImageView放在工具栏中。例如: -

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/material_blue"
        android:elevation="5dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:theme="@style/ToolbarCustomIconColor" >
             <RelativeLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_content" >
                 <ImageView
                     android:id="@+id/imgLogo"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_centerInParent="true"
                     android:src="@drawable/your_icon" />
         </RelativeLayout>
    </android.support.v7.widget.Toolbar>

您可以像这样创建自定义工具栏,并根据需要对其进行操作。

相关问题