我正在使用 constraintLyout v 1.0.1。
我想在我的 xml 中包含一个子 ConstraintLayout,它对应于我的全局布局的一部分(它本身就是一个 ConstraintLayout)。我将布局拆分为两个 xml,以便在其他地方使用此子部分
我试过这个,但我无法控制将子约束布局放置在父级中的位置。我想知道我是否必须将所有内容放在同一个 xml 文件中,或者它们是否是使用单独文件的解决方案。
tmp_1.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LABEL1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
/>
<TextView
android:id="@+id/label_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LABEL2"
app:layout_constraintStart_toStartOf="@id/label"
app:layout_constraintEnd_toEndOf="@id/label"
app:layout_constraintTop_toBottomOf="@id/label"
android:layout_marginTop="16dp"
/>
<include layout="@layout/tmp_2" />
</android.support.constraint.ConstraintLayout>
tmp_2.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/view_80"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="80th element"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="10dp"
android:layout_marginStart="12dp"
/>
</android.support.constraint.ConstraintLayout>
结果是这样的
但我希望它是这样的
我试过了,但是不行
<include
app:layout_constraintTop_toBottomOf="@id/label_2"
layout="@layout/tmp_2" />
最佳答案
其实找到了解决办法。 Android Studio 不会自动完成 include 标记中的 constraintLayout 参数,但只要您为其提供大小,它们就会对其产生影响。
<include
layout="@layout/tmp_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/label_2"
/>
关于android - 如何将约束布局包含到另一个约束布局并在每个约束布局之间设置约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43676415/