工具栏不在AppBarLayout的中心 [英] Toolbar isn't in the center of the AppBarLayout

查看:87
本文介绍了工具栏不在AppBarLayout的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此示例构建具有两个NavigationViews的布局:

I used this example to build a layout with two NavigationViews:

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

工作正常.而且,在此站点的演示中,您还可以看到,工具栏并不是100%位于中间.左按钮比右按钮的右边框具有更多的空间.

It works fine. But also in the demo on this site you can see, that the Toolbar isn't 100% in the middle. The left button has more space to the left than the right button to the right border.

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

推荐答案

添加

app:contentInsetStart="0dp" 

到您的工具栏.这将删除工具栏左侧的多余填充

to your toolbar.This will remove the extra padding in the left of the toolbar

这篇关于工具栏不在AppBarLayout的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆