使用 ConstraintLayout 创建背景

The*_*mer 3 android android-constraintlayout

我想创建一个类似盒子的东西,其中包含按钮和标签,并具有白色稀松布,通常通过以下方式完成:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentBottom=true
        android:background="#99ffffff
        android:gravity="center_horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button Text"
            android:layout_margin="8dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Label Text"
            android:layout_margin="8dp"/>
    </LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我所做的是:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <View
        android:id="@+id/scrim
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#99ffffff"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button"/>
    <Button
        android:id="@+id/button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/label"/>
    <TextView
        android:id="@+id/label
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

这个想法是创建一个底部约束到父级、顶部约束到按钮顶部的稀松布视图,按钮位于 TextView 顶部,而 TextView 约束于父级底部。

问题在于稀松布不尊重按钮的边距,即稀松布的顶部与按钮的顶部齐平,而不是在其上方 8dp。

Nic*_*ard 5

您得到的结果是预期的 - 您告诉您的稀松布视图将其顶部限制到按钮的顶部 - 这里不包含边距。约束连接到目标,并且除此之外还可以具有(正)裕度;边距仅适用于现有连接。

如果我们可以使用负边距,您可以在稀松布视图的顶部约束上设置一个。唉,目前还没有授权。您可以做的是使用 Space 视图来模拟它 - 将其约束到按钮的顶部,然后将稀松布视图的顶部约束到 Space 视图的顶部,如下所示:

<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/scrim"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#99ff00ff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/topButton" />

    <Space
        android:id="@+id/topButton"
        android:layout_width="8dp"
        android:layout_height="8dp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintLeft_toLeftOf="@+id/button" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="Button Text"
        app:layout_constraintBottom_toTopOf="@+id/label"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="Label Text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)