【问题标题】:ConstraintLayout takes extra space around it when it is included inside Toolbar (android.support.v7.widget.Toolbar) widget当 ConstraintLayout 包含在 Toolbar (android.support.v7.widget.Toolbar) 小部件中时,它会占用额外的空间
【发布时间】: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


    【解决方案1】:

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

    app:contentInsetStartWithNavigation="0dp"
    

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

    【讨论】:

    • 但是先生,这个属性对我不起作用。添加此属性之前我应该​​做什么?
    • 在工具栏元素上添加app:contentInsetStart="0dp" 可以解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    相关资源
    最近更新 更多