Kél*_*ian 42 xml android include android-layout android-constraintlayout
我正在使用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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
结果是此实际结果
但我希望它成为预期的结果
我试过这个,但它不起作用
<include
app:layout_constraintTop_toBottomOf="@id/label_2"
layout="@layout/tmp_2" />
Run Code Online (Sandbox Code Playgroud)
我很乐意有你的解决方案,
谢谢
Kél*_*ian 77
实际上找到了解决方案.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"
/>
Run Code Online (Sandbox Code Playgroud)
Dhr*_*rma 21
为了包含一个约束布局并根据需要对其进行约束,必须为包含的布局提供宽度和高度,如下所示:
<include
android:id="@+id/shop_card_layout"
layout="@layout/shop_card_one"
android:layout_height="wrap_content"
android:layout_width="300dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="@id/heading_tv"
app:layout_constraintTop_toBottomOf="@+id/heading_tv" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18813 次 |
| 最近记录: |