如何将约束布局包含到另一个约束布局中并在每个布局之间设置约束 [英] How to include constraint layout to another constraint layout and set constraint between each

查看:206
本文介绍了如何将约束布局包含到另一个约束布局中并在每个布局之间设置约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用constraintLyout v 1.0.1。

I'm using constraintLyout v 1.0.1.

我想在我的xml中包含一个与我的全局布局的一部分相对应的子ConstraintLayout(它本身是ConstraintLayout)。我将布局拆分为两个xml以便在其他地方使用这个子部分

I would like to include in my xml a sub ConstraintLayout corresponding to a part of my global layout (which itself is a ConstraintLayout). I split the layout in two xmls in order to use this sub part elsewhere

我试过这个但我无法控制将子约束布局放在父级中的位置。我想知道是否必须将所有内容放在同一个xml文件中,或者它们是否是使用单独文件的解决方案。

I tried this but I have no control on where to place my sub constraint layout in the parent. I wonder if I have to place everything in the same xml file or if their is a solution to use separate files.

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>

结果是实际结果

但我希望它是这个预期结果

我试过这个但它不起作用

I tried this but it does not work

<include 
    app:layout_constraintTop_toBottomOf="@id/label_2"
    layout="@layout/tmp_2" />

我很乐意为您提供解决方案,

I would be glad to have your solutions,

谢谢

推荐答案

实际上找到了解决方案。
Android Studio不会在include标记中自动填充constraintLayout参数,但只要您提供包含大小的内容,它们就会对它产生影响。

Actually found a solution. Android Studio does not autocomplete constraintLayout parameters in an include tag but they do have an impact on it as long as you give that include a size.

<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"
        />

这篇关于如何将约束布局包含到另一个约束布局中并在每个布局之间设置约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆