如何将约束布局包含到另一个约束布局中并在每个布局之间设置约束

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)

  • @NoumanBhatti你可能会误会.ConstraintLayout正在减少嵌套布局,而不是在没有嵌套布局的情况下创建所有布局.Include正在重复使用布局,这就是ConstraintLayout无法做到的事情.如果包含一个包含可以使您的布局更灵活,您可以毫不犹豫地完成. (4认同)
  • 如果我们在另一个约束布局中包含约束布局,您不认为它会破坏创建没有嵌套布局的 UI 的目的吗? (2认同)
  • 对于任何落后于我的人,如果这不起作用,可能是因为你已经将你的包嵌套在`<merge />`标签中.当我删除它并将约束布局留给父布局时,我能够调整定位和大小 (2认同)
  • 同样对于我的设置,我不能使用 wrap_content 作为高度,我必须给它一个明确的 dp 值。 (2认同)

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)

  • 正确答案!我添加了layout_width和layout_height,设置包含(在我的例子中是顶部和底部)并且它有效。 (2认同)