【发布时间】:2017-06-26 08:03:08
【问题描述】:
今天我遇到了一个意想不到的问题。当尝试在 Toolbar(android.support.v7.widget.Toolbar) 小部件中使用 ConstraintLayout 时,它会在它周围占用额外的空间。当我在像 LeanearLayout 这样的花药布局中使用它(具有相同 xml 代码的 ConstraintLayout)时,它工作正常(它周围不会意外占用任何空间)。
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的设计视图如下:
当我在 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的设计视图如下:
注意:这里黄色是工具栏小部件的背景。额外的黄色背景显示了意想不到的空间。
现在我的问题: 是否可以在 Toolbar 内使用 ConstraintLayout 以便它可以覆盖其父级(工具栏)的整个主体? 如果可能的话如何解决我现在面临的问题?
任何回答和建议都会受到表扬。
【问题讨论】:
标签: toolbar android-coordinatorlayout android-constraintlayout