如何将约束布局包含到另一个约束布局中,并设置它们之间的约束关系?

91

我正在使用ConstraintLayout v 1.0.1。

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

我尝试过这样做,但我无法控制在父元素中放置我的子constraint layout的位置。我想知道我是否必须将所有内容放在同一个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" />

您可以将约束参数添加到包含布局的根标记中。 - opLW
5个回答

178

实际上找到了解决方案。 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"
        />

2
你不觉得如果我们在另一个约束布局中包含约束布局,这会破坏创建UI时避免嵌套布局的目的吗? - Nouman Bhatti
13
@NoumanBhatti,你可能误解了。ConstraintLayout的目标是减少嵌套布局而不是完全消除嵌套布局。Include的目标是重用布局,而这正是ConstraintLayout无法做到的。如果包含一个include可以使您的布局更加灵活,那么您可以毫不犹豫地这样做。 - Allen Vork
3
对于后来的任何人,如果这个不起作用,可能是因为你将你的包含文件嵌套在了一个<merge />标签中。当我移除它并将约束布局设置为父布局时,我能够调整位置和大小。 - hallmanitor
2
对于我的设置,我不能使用wrap_content来设置高度,我必须给它一个明确的dp值。 - hallmanitor
1
这个解决方案也适用于另一种情况。似乎当你指定任何app:layout_constraint*参数时,Android Studio期望你还必须为<include>指定android:layout_widthandroid:layout_height - Michael Osofsky
显示剩余2条评论

81

为了包含一个约束布局并根据需要进行约束,必须向所包含的布局提供宽度和高度,如下所示:

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

如果重新定义ID,那么就会出现这个错误 https://dev59.com/v1YO5IYBdhLWcg3wTPvk - Deepak Kumar
4
正确答案!我添加了layout_width和layout_height,设置了约束(在我的情况下是上下),它就可以工作了。 - oxied
这是正确的答案,需要在包含的布局中添加高度和宽度,不需要再包裹在另一个父布局中。 - Nezneika
谢谢!将 W 和 H 添加到子布局中让我可以对其进行约束。 - sebasira

1
你可以在include项中避免使用ConstraintLayout约束。我只是按原样它。
MainActivity布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/toolbarLayout"
        layout="@layout/layout_toolbar" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="CONTENTS"
        app:layout_constraintBottom_toBottomOf="@+id/footerLayout"
        app:layout_constraintEnd_toEndOf="@+id/footerLayout"
        app:layout_constraintStart_toStartOf="@+id/footerLayout"
        app:layout_constraintTop_toTopOf="@+id/footerLayout" />

    <include
        android:id="@+id/footerLayout"
        layout="@layout/layout_footer" />

</android.support.constraint.ConstraintLayout>

工具栏布局文件:

<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <TextView
            android:id="@+id/toolbarTitleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/hidden"
            android:textColor="@android:color/white"
            tools:layout_editor_absoluteX="192dp"
            tools:layout_editor_absoluteY="19dp" />

    </android.support.v7.widget.Toolbar>
</android.support.constraint.ConstraintLayout>

使用include会在父约束布局中创建一个约束布局,那么使用include还有意义吗?因为你不再拥有约束布局的扁平视图层次结构。 - 11m0
通过“包含”另一个布局,您可以在其他“活动”中重用已包含的布局。 - LiTTle
因此,优点在于能够重用特定的布局文件,但缺点是您可能会遭受性能损失,因为布局可能会被嵌套。这样对吗? - 11m0
是的,你有一个性能问题。但是,我认为我找到了整个问题的解决方案。解决方案可以是在这里描述的parentTag(https://android.jlelse.eu/android-dev-tip-5-55226527e780)。我会在有时间时进行测试,或者您可以测试并让社区知道。 - LiTTle
tools 中的 parentTag 只是用于 Android Studio 设计选项卡中所见内容,它在运行时没有任何影响。 - Nikos Hidalgo

-1
如果使用约束布局,我们可以使用布局标签。对于线性布局,我们可以直接包含布局。
我有一个不同的需求,在工具栏下面有一个选项卡视图。我将工具栏高度设置为0dp,并在选项卡视图下方添加了一个视图文件,以便选项卡视图和工具栏看起来像一个单元。
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs_container"
        style="@style/tab_layout_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:tabGravity="fill"
        app:tabIndicatorFullWidth="false"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/text_black_app"
        app:tabTextColor="@color/txt_gray_contact_lbl" />

    <LinearLayout
        android:id="@+id/layout_elevation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/tabs_container">

        <include layout="@layout/gray_view_for_elevation" />
    </LinearLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHeight_percent="100"
        app:layout_constraintTop_toBottomOf="@+id/layout_elevation_view" />

</androidx.constraintlayout.widget.ConstraintLayout>

这段代码是用于线性布局的

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    android:orientation="vertical">

    <com.google.android.material.tabs.TabLayout
        style="@style/tab_layout_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="@dimen/dp_4"
        app:tabGravity="fill"
        app:tabIndicatorFullWidth="false"
        app:tabMode="scrollable"/>

    <include
        android:id="@+id/layout_elevation_view"
        layout="@layout/gray_view_for_elevation" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_gray" />

</LinearLayout>

我的高程视图文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<View
    android:layout_width="wrap_content"
    android:layout_height="@dimen/dp_1"
    android:background="#80bbbbbb" />

<View
    android:layout_width="wrap_content"
    android:layout_height="@dimen/dp_1"
    android:background="#60bbbbbb" />

<View
    android:layout_width="wrap_content"
    android:layout_height="@dimen/dp_1"
    android:background="#50bbbbbb" />

希望这能有所帮助。

-1

将一个约束布局包含到另一个约束布局中,使用一个以上的约束布局来包含每个包含布局的父布局... 就像下面这样:

<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/new_landing_bg"
tools:context=".activity.DesignTestActivity">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <include layout="@layout/common_footer_layout" />

</android.support.constraint.ConstraintLayout>

这是针对我的 XML 布局的工作。享受编码吧。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接