Android 如何在ConstraintLayout中放置背景形状?
我嵌套了Android 如何在ConstraintLayout中放置背景形状?,android,android-linearlayout,android-constraintlayout,android-shape,Android,Android Linearlayout,Android Constraintlayout,Android Shape,我嵌套了linearlayout,阅读了一些文档后,我尝试切换到ConstraintLayout。但我没有找到如何插入视图以仅显示背景。 到目前为止,我只是在包含项目的布局上设置背景 这是旧嵌套结构的外观(在其他子布局中有更多嵌套项): ... 通过这种方式,我可以轻松地为我显示的不同项目组设置自己的形状作为背景 如何使用ConstraintLayout实现等效性 我了解如何约束我的项目以同样的方式组织,以及如何设置背景以与所有内容对齐。但是我如何插入一个简单的背景项目呢?我使用ImageV
linearlayout
,阅读了一些文档后,我尝试切换到ConstraintLayout
。但我没有找到如何插入视图以仅显示背景。到目前为止,我只是在包含项目的布局上设置背景 这是旧嵌套结构的外观(在其他子布局中有更多嵌套项):
...
通过这种方式,我可以轻松地为我显示的不同项目组设置自己的形状作为背景
如何使用ConstraintLayout
实现等效性
我了解如何约束我的项目以同样的方式组织,以及如何设置背景以与所有内容对齐。但是我如何插入一个简单的背景项目呢?我使用
ImageView
来显示我的背景,并且(几乎)找到了一种方法来将我的项目组合在其中。一个问题是,我得到一个没有contentDescription的警告
图像
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">
<ImageView
android:id="@+id/background"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="2:5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="@drawable/global_background" />
...
<Button
android:id="@+id/buttonValidate"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="8dp"
android:background="@drawable/green_button"
android:text="@string/validate"
app:layout_constraintBottom_toBottomOf="@+id/background"
app:layout_constraintDimensionRatio="15:6"
app:layout_constraintEnd_toEndOf="@+id/background"
app:layout_constraintStart_toStartOf="@+id/background"
app:layout_constraintTop_toBottomOf="@+id/upperButton" />
...
但我很难在里面放置另一个背景(它本身包含几个TextView
)。我的约束总是把其中一个弄得一团糟。。。我最终得到了一个嵌套的ConstraintLayout
,它工作得很好,可能不是最佳解决方案,但我所有的其他尝试都失败了
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">
<ImageView
android:id="@+id/background"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="2:5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="@drawable/global_background" />
...
<Button
android:id="@+id/buttonValidate"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="8dp"
android:background="@drawable/green_button"
android:text="@string/validate"
app:layout_constraintBottom_toBottomOf="@+id/background"
app:layout_constraintDimensionRatio="15:6"
app:layout_constraintEnd_toEndOf="@+id/background"
app:layout_constraintStart_toStartOf="@+id/background"
app:layout_constraintTop_toBottomOf="@+id/upperButton" />