[英]How to put background shapes in ConstraintLayout?
我嵌套了LinearLayouts
並在閱讀了一些文檔后嘗試切換到ConstraintLayout
。 但我沒有找到如何插入視圖以僅顯示背景。
到目前為止,我只是在包含項目的布局上設置了背景。
這是舊的嵌套結構的樣子(在其他子布局中有更多嵌套項):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/black"
android:gravity="center" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
... some margins and padding
android:background="@drawable/global_background" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:background="@drawable/text_area_background" >
<TextView
android:id="@+id/Text1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:id="@+id/Text2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center" />
...
通過這種方式,我能夠輕松地將自己的形狀設置為我顯示的不同項目組的背景。
如何使用ConstraintLayout
實現等效?
我了解如何限制我的項目以相同的方式組織,以及如何設置背景以與所有內容保持一致。 但是如何插入一個簡單的背景項目?
我使用ImageView
來顯示我的背景,並且(幾乎沒有)找到一種將我的物品組裝在其中的方法。
一個問題是我收到一個Image without 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
,它工作正常,可能不是最佳解決方案,但我所有的其他嘗試都失敗了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.