简体   繁体   中英

Android Toolbar Image Centered with Nav Drawer button

I have toolbar with image as logo, the logo was centered. But when I put navigation drawer, the logo moved to right because there was navigation drawer button. How to make the logo always centered with navigation drawer button in toolbar?

Thanks.

My code

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

try this layout: add android:layout_gravity="center_horizontal" to the 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>

Use android:contentInsetLeft="0dp" OR android:contentInsetStart="0dp" with Toolbar

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM