使用ConstraintLayout创建背景图案

3

我想创建一个类似于包含按钮和标签的框,并具有白色遮罩层的东西,通常可以通过以下方式完成:

<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>

我的工作内容如下:

<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>

这个想法是创建一个 scrim View,在底部与父视图相连,在顶部与按钮的顶部相连。这个按钮位于一个 TextView 的上方,而这个 TextView 则与父视图的底部相连。

问题在于,scrim 不考虑按钮的 margin,也就是说它的顶部与按钮的顶部齐平,而不是距离它 8dp 以上。

1个回答

5
你得到的结果是预期的——你告诉 scrim View 将其顶部约束到按钮的顶部,这里不包括边距。约束连接到目标,并且可以在其上添加(正)边距;边距仅适用于现有的连接。
如果我们能使用负边距,你可以在 scrim view 的顶部约束上设置一个负边距。可惜,目前还不能这样做。相反,你可以使用 Space view 来模拟它——将其约束到按钮的顶部,然后将 scrim view 的顶部约束到 Space view 的顶部,如下所示:
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接