Android工具栏图片以Nav Drawer按钮为中心

时间:2017-02-14 07:34:34

标签: android image center toolbar

我的工具栏上有图片作为徽标,徽标居中。但是当我放置导航抽屉时,徽标向右移动,因为有导航抽屉按钮。如何使徽标始终以工具栏中的导航抽屉按钮为中心?

感谢。

我的代码

<android.support.design.widget.AppBarLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
       android:id="@+id/toolbar"
       android:layout_width="match_parent"
       android:layout_height="?attr/actionBarSize"
       android:background="?attr/colorPrimary"
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

           <ImageView
              android:layout_width="match_parent"
              android:layout_height="25dp"
              android:src="@drawable/logo"/>

     </android.support.v7.widget.Toolbar>

     <android.support.design.widget.TabLayout
         android:id="@+id/tabs"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="?attr/colorPrimaryDark"
         app:tabGravity="fill"
         app:tabMode="fixed" />

</android.support.design.widget.AppBarLayout>

2 个答案:

答案 0 :(得分:2)

尝试此布局:将android:layout_gravity="center_horizontal"添加到Imageview

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

            <ImageView
                android:layout_width="match_parent" //change this to wrap_content
                android:layout_height="25dp"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/logo"/>
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimaryDark"
            app:tabGravity="fill"
            app:tabMode="fixed" />
    </android.support.design.widget.AppBarLayout>

答案 1 :(得分:1)

使用工具栏 android:contentInsetLeft =“0dp” android:contentInsetStart =“0dp”

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:contentInsetLeft="0dp">