当它包含在工具栏(android.support.v7.widget.Toolbar)小部件中时,ConstraintLayout会占用额外的空间

时间:2017-06-26 08:03:08

标签: toolbar android-coordinatorlayout android-constraintlayout

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

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的设计视图:

ConstraintLayout desing mode picture

当我在my_toolbar.xml中包含此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的设计视图:

enter image description here

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

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

任何答案和建议都会受到赞扬。

1 个答案:

答案 0 :(得分:3)

这是设计支持库版本 24.0.0 中引入的新 Material Design Specification 的一部分。 可以通过向Toolbar小部件添加以下属性来删除额外空间。

app:contentInsetStartWithNavigation="0dp"

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