工具栏子节点不居中

Toolbars child node does not center

我不好意思问这个问题,但我遇到了很多麻烦,似乎无法弄清楚为什么。

也就是说,我正在尝试将 LinearLayout 置于工具栏的中心,但我一直无法弄清楚为什么它不起作用。

谁能看出我哪里出错了?

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="?attr/actionBarSize"
     android:background="@color/white"
     android:focusable="true"
     android:focusableInTouchMode="true"
     android:gravity="center"
     android:orientation="horizontal"
     android:id="@+id/toolbar">

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_gravity="center">
</LinearLayout>

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

此代码产生以下结果: picture of the problem

使用包装器视图将工具栏内的任何内容居中:

<android.support.v7.widget.Toolbar
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="?attr/actionBarSize"
 android:background="@color/white"
 android:focusable="true"
 android:focusableInTouchMode="true"
 android:gravity="center"
 android:orientation="horizontal"
 android:id="@+id/toolbar">

 <FrameLayout
  android:layout_height="match_parent"
  android:layout_width="match_parent">
    <TextView 
        text="Something"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />
 </FrameLayout>

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

左边的插图是 ToolBar's contentInsetStart,它具有默认尺寸。您可以使用 xml.

设置为 0dp
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/white"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"
    app:contentInsetStart="0dp"
    android:orientation="horizontal"
    android:id="@+id/toolbar">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </LinearLayout>

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