简体   繁体   English

工具栏不在AppBarLayout的中心

[英]Toolbar isn't in the center of the AppBarLayout

I used this example to build a layout with two NavigationViews: 我使用此示例构建具有两个NavigationViews的布局:

http://v4all123.blogspot.de/2016/03/simple-example-of-navigation-view-on.html http://v4all123.blogspot.de/2016/03/simple-example-of-navigation-view-on.html

It works fine. 工作正常。 But also in the demo on this site you can see, that the Toolbar isn't 100% in the middle. 而且,在此站点的演示中,您可以看到工具栏不是100%位于中间。 The left button has more space to the left than the right button to the right border. 左边的按钮比右边的右按钮具有更多的空间。

Take a look to the screenshot from Android Studio. 看一下Android Studio中的屏幕截图。

I've tried to center each view but the only way to get the same space left and right is to add a padding to the right. 我试图将每个视图居中,但获得左右相同空间的唯一方法是在右边添加填充。 But I can't believe that this is the only way to get it to the middle of the layout. 但是我无法相信这是将其移到布局中间的唯一方法。

Is there another trick to center the buttons and views in the Toolbar? 还有其他技巧可以使工具栏中的按钮和视图居中吗?

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:context=".frontend.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    app:elevation="0dp"
    android:gravity="center">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

            <ImageButton
                android:id="@+id/menuLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:src="@drawable/ic_menu_white_24dp"
                android:tint="@color/colorPrimary" />
            <TextView
                android:id="@+id/headerLine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="Example application"
                android:textSize="20dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:textAlignment="center"
                android:maxLines="1"
                android:lines="1"
                android:layout_toRightOf="@+id/menuLeft"
                android:layout_toLeftOf="@+id/menuRight"
                android:singleLine="true"
                android:ellipsize="end"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textColor="@color/headerTextColorPrimary" />
            <ImageButton
                android:id="@+id/menuRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:src="@drawable/ic_menu_white_24dp"
                android:tint="@color/colorPrimary" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

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

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

add

app:contentInsetStart="0dp" 

to your toolbar.This will remove the extra padding in the left of the toolbar 到工具栏。这将删除工具栏左侧的多余填充

It is already answered here. 这里已经回答了。 Please find android:contentInsetLeft="0dp" and android:contentInsetStart="0dp" in following link. 请在以下链接中找到android:contentInsetLeft="0dp"android:contentInsetStart="0dp" This will work for you. 这将为您工作。

https://stackoverflow.com/a/32320632/4688535 https://stackoverflow.com/a/32320632/4688535

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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