我有一个重复的布局组件,如下所示:
<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/widgetSettingsEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imgEntryIcon"
android:layout_width="40dp"
android:layout_height="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvEntryName"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@id/imgEntryIcon"
app:layout_constraintEnd_toStartOf="@+id/tvEntryValue"
app:layout_constraintStart_toEndOf="@+id/imgEntryIcon"
app:layout_constraintTop_toTopOf="@id/imgEntryIcon" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvEntryValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@id/imgEntryIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/imgEntryIcon" />
</androidx.constraintlayout.widget.ConstraintLayout>
当尝试多次将以上内容包含在主布局中时,使用include
关键字将布局包含在主布局中(以下),它将相互覆盖。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layoutUserSettings"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/line1"
layout="@layout/widget_settings_entry"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<view
android:id="@+id/vb1"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@color/colorRatingBarFill"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line1" />
<include
android:id="@+id/line2"
layout="@layout/widget_settings_entry"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/vb1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>