在ConstraintLayout中向<include>添加约束

时间:2017-09-14 05:58:39

标签: android android-layout android-constraintlayout

请查看我的xml:

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize">

    <RelativeLayout
        android:id="@+id/layout_info"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <include
        layout="@layout/page"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

</android.support.constraint.ConstraintLayout>

我无法向<include>添加约束。 app命名空间不起作用(适用于RelativeLayout),自动填充不显示约束属性。我希望包含的布局高度为ConstraintLayout中的剩余空间,但我如何在没有约束的情况下执行此操作!请帮忙。

3 个答案:

答案 0 :(得分:8)

试试这个

 <android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize">

    <RelativeLayout
        android:id="@+id/layout_info"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <include
         layout="@layout/page"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

  

试试这个:

<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:id="@+id/constraintLayout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_marginTop="?attr/actionBarSize">

<RelativeLayout
    android:id="@+id/layout_info"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />

<include
    layout="@layout/page"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

答案 2 :(得分:0)

<include layout="@layout/layout_no_friends_avalable"
                 android:id="@+id/inc_no_friend"
                 android:layout_width="match_parent"
                 android:layout_height="0dp"
                 app:layout_constraintBottom_toBottomOf="parent"
                 app:layout_constraintEnd_toEndOf="parent"
                 app:layout_constraintTop_toBottomOf="@+id/toolbar_main"
        />

<include>标签中添加约束时,我们必须同时使用android:layout_widthandroid:layout_height这两个属性。