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