gpt4 book ai didi

android - 使用 ConstraintLayout 创建背景

转载 作者:太空宇宙 更新时间:2023-11-03 13:12:39 25 4
gpt4 key购买 nike

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

<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 ,该 View 的底部受约束到父级,顶部受限于按钮的顶部,一个按钮位于 TextView 的顶部,而 TextView 的约束在父级的底部。

问题是稀松布不考虑按钮的边距,即稀松布的顶部与按钮的顶部齐平,而不是高出按钮顶部 8dp。

最佳答案

您得到的结果是预期的——您告诉您的 scrim View 将其顶部限制在按钮的顶部——这里不包括边距。一个约束连接到一个目标,并且除了它之外还可以有一个(正的)边距;边距将仅适用于现有连接。

如果我们可以使用负边距,您可以在稀松布 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>

关于android - 使用 ConstraintLayout 创建背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41960670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com