当 ConstraintLayout 包含在 Toolbar (android.support.v7.widget.Toolbar) 小部件中时,它周围需要额外的 space

ConstraintLayout takes extra space around it when it is included inside Toolbar (android.support.v7.widget.Toolbar) widget

今天我遇到了一个意想不到的问题。当尝试在 Toolbar(android.support.v7.widget.Toolbar) 小部件中使用 ConstraintLayout 时,当我使用它时 nothing.And 在它周围需要额外的 space (ConstraintLayout 具有相同的 xml 代码)在像 LeanearLayout 这样的花药布局中它工作正常(它不会在它周围意外地占用任何 space)。

my_inside_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="wrap_content"
    android:background="#f90511"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="81dp">


    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:background="#000"
        android:text="Button"
        android:textColor="#fff"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button5"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:background="#0b8653"
        android:text="Button"
        android:textColor="#fff"
        app:layout_constraintLeft_toRightOf="@+id/button4"
        app:layout_constraintRight_toLeftOf="@+id/button6"
        app:layout_constraintTop_toTopOf="@+id/button4" />

    <Button
        android:id="@+id/button6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:background="#000"
        android:text="Button"
        android:textColor="#fff"
        app:layout_constraintLeft_toRightOf="@+id/button5"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button5" />
</android.support.constraint.ConstraintLayout>

上面xml下面的设计视图:

当我将此 xml 包含在 my_toolbar.xml 中时,问题出现了。

这是 my_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#f5e903">

    <include
        android:id="@+id/la"
        layout="@layout/my_inside_toolbar.xml" />

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

上面xml下面的设计视图:

注意:此处黄色是工具栏小部件的背景。额外的黄色背景显示出意想不到的 space。

现在我的问题是: 是否可以在 Toolbar 中使用 ConstraintLayout 以便它可以覆盖其父项(工具栏)的整个主体? 如果可以的话如何解决我现在面临的问题?

任何回答和建议都会点赞

这是新 Material 设计规范 的一部分,在 24.0.0 版本中引入设计支持库。 可以通过将以下 属性 添加到工具栏小部件来删除额外的 space。

app:contentInsetStartWithNavigation="0dp"

@kocus 在评论中提到添加 app:contentInsetStart="0dp" 解决了他的问题,我最初误解了这个问题。