为什么链条元素之间的空间如此之大?

时间:2018-03-12 12:48:25

标签: android android-constraintlayout

我需要将一行三个按钮对齐到背景视图,如下所示:

enter image description here

为此,我创建了一个背景视图(@drawable/background)和一个链(@id/button1@id/button2@id/button3

activity_main.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/background"
        android:layout_width="0dp"
        android:layout_height="32dp"
        android:layout_marginEnd="48dp"
        android:layout_marginStart="48dp"
        android:layout_marginTop="16dp"
        android:background="@drawable/background"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="24dp"
        android:layout_marginStart="4dp"
        android:background="@drawable/button_background"
        android:gravity="center"
        android:text="Button1"
        app:layout_constraintBottom_toBottomOf="@id/background"
        app:layout_constraintEnd_toStartOf="@id/button2"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="@id/background"
        app:layout_constraintTop_toTopOf="@id/background" />

    <TextView
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="24dp"
        android:background="@drawable/button_background"
        android:gravity="center"
        android:text="Button2"
        app:layout_constraintBottom_toBottomOf="@id/background"
        app:layout_constraintEnd_toStartOf="@id/button3"
        app:layout_constraintStart_toEndOf="@id/button1"
        app:layout_constraintTop_toTopOf="@id/background" />

    <TextView
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="24dp"
        android:layout_marginEnd="4dp"
        android:background="@drawable/button_background"
        android:gravity="center"
        android:text="Button3"
        app:layout_constraintBottom_toBottomOf="@id/background"
        app:layout_constraintEnd_toEndOf="@id/background"
        app:layout_constraintStart_toEndOf="@id/button2"
        app:layout_constraintTop_toTopOf="@id/background" />

</android.support.constraint.ConstraintLayout>

background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="16dp" />
    <solid android:color="#11000000" />
</shape>

button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="12dp" />
    <stroke android:width="2dp" android:color="@color/colorPrimary" />
    <solid android:color="#ffffff" />
</shape>

但是我得到以下结果:

enter image description here

我无法弄清楚为什么视图之间的空间如此之大。如documentation中所述对链进行加权。在第一个视图之前和最后一个视图之后应该有4dp边距,按钮之间根本没有空格。

更新

我已经尝试过版本1.0.2以及约束布局的所有测试版本,我只能在constraint-layout:1.1.0-beta5中重现该问题。是否意味着此版本的约束布局中存在错误,或者我是否以错误的方式使用它?

更新2

这实际上是测试版5中的错误,在测试版6中是fixed

1 个答案:

答案 0 :(得分:1)

我不知道为什么这种情况正在发生,也许这确实是1.1.0-beta5中的一个错误,但是......

您应用于background视图的开始/结束边距由三个按钮链继承,因为该链仅限于background的开头和结尾。如果删除这些边距,突然间会出现这些差距。消失:

<View
    android:id="@+id/background"
    android:layout_width="0dp"
    android:layout_height="32dp"
    android:layout_marginTop="16dp"
    android:background="@drawable/background"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

enter image description here

当然,您可能需要这些边距用于您的设计。您可以通过将原始边距恢复到background视图并将链锚定到父级而不是背景来解决此问题。也就是说,将app:layout_constraintStart_toStartOf上的button1属性更改为"parent",并将app:layout_constraintEnd_toEndOf上的button3属性更改为"parent"

enter image description here

最后一个难题是更改的开始/结束边距以模仿旧行为。以前,您的background视图的边距为48dp,边距为4dp。因此,将链边距设置为52dp而不是4dp以包含背景48dp:

enter image description here

我的完整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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/background"
        android:layout_width="0dp"
        android:layout_height="32dp"
        android:layout_marginTop="16dp"
        android:layout_marginStart="48dp"
        android:layout_marginEnd="48dp"
        android:background="@drawable/background"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <TextView
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="24dp"
        android:layout_marginStart="52dp"
        android:gravity="center"
        android:text="Button1"
        android:background="@drawable/button_background"
        app:layout_constraintTop_toTopOf="@id/background"
        app:layout_constraintBottom_toBottomOf="@id/background"
        app:layout_constraintEnd_toStartOf="@id/button2"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"/>

    <TextView
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="24dp"
        android:gravity="center"
        android:text="Button2"
        android:background="@drawable/button_background"
        app:layout_constraintTop_toTopOf="@id/background"
        app:layout_constraintBottom_toBottomOf="@id/background"
        app:layout_constraintEnd_toStartOf="@id/button3"
        app:layout_constraintStart_toEndOf="@id/button1"/>

    <TextView
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="24dp"
        android:layout_marginEnd="52dp"
        android:gravity="center"
        android:text="Button3"
        android:background="@drawable/button_background"
        app:layout_constraintTop_toTopOf="@id/background"
        app:layout_constraintBottom_toBottomOf="@id/background"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/button2"/>

</android.support.constraint.ConstraintLayout>
相关问题