实现控件沾满屏幕
<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:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ImageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="H,1:2"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.edit.EditActivity">
<ImageView
android:id="@+id/iv_download"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_marginBottom="40dp"
android:background="@mipmap/icon_download"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/iv_apply"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/iv_apply"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_marginBottom="40dp"
android:background="@mipmap/icon_apply"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/iv_collect"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toEndOf="@id/iv_download" />
<ImageView
android:id="@+id/iv_collect"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_marginBottom="40dp"
android:background="@mipmap/icon_collect"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toEndOf="@id/iv_apply" />
</androidx.constraintlayout.widget.ConstraintLayout>
androidx.constraintlayout.widget.Group管理多个控件
androidx.constraintlayout.widget.Group
是 AndroidX ConstraintLayout 库中的一个控件,用于管理多个视图的可见性状态。通过使用 Group
,您可以在一个组内同时控制多个视图的可见性,而不需要逐个处理每个视图的可见性设置。
布局示例
<androidx.constraintlayout.widget.Group
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="textView1,textView2,textView3" />
<TextView
android:id="@+id:textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 1"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id:textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 2"
app:layout_constraintTop_toBottomOf="@id:textView1" />
<TextView
android:id="@+id:textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 3"
app:layout_constraintTop_toBottomOf="@id:textView2" />
在上述示例中,我们将三个 TextView
包装在一个 Group
控件内,并通过 app:constraint_referenced_ids
属性指定了这些视图的 ID。这样,我们就可以通过控制 Group
控件的可见性来同时控制这三个 TextView
的可见性。
1.在您的代码中,您可以通过调用 Group
控件的 setVisibility
方法来控制其中包含的视图的可见性。例如:
val group = findViewById<Group>(R.id.group)
group.visibility = View.GONE // 设置 Group 控件以及其内部的视图都不可见
通过上述操作,您可以使用 Group
控件来方便地管理多个视图的可见性状态,从而简化代码并提高代码的可维护性