Android 约束布局未按预期工作

use*_*444 7 xml android android-layout android-constraintlayout

我有一个根标签为

androidx.constraintlayout.widget.ConstraintLayout
Run Code Online (Sandbox Code Playgroud)

我在此活动中包含 3 个布局文件并垂直列出它们。以下对中间的约束不起作用

    app:layout_constraintTop_toBottomOf="@+id/lay1"
    app:layout_constraintBottom_toTopOf="@+id/lay3"
Run Code Online (Sandbox Code Playgroud)

我不明白为什么中间一个占据整个屏幕而不是从顶部的底部开始并在底部的顶部结束。你能帮我么?

活动:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/lay1"
        layout="@layout/empty_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <include
        android:id="@+id/lay2"
        layout="@layout/empty_linear_layout"
        app:layout_constraintBottom_toTopOf="@+id/lay3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/lay1" />

    <include
        android:id="@+id/lay3"
        layout="@layout/empty_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

empty_linear_layout.xml :

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="button" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Ben*_* P. 11

如果要覆盖布局layout_上的参数<include>,则必须同时指定layout_widthlayout_height。从开发人员文档

但是,如果要使用<include>标记覆盖布局属性,则必须同时覆盖android:layout_heightandroid:layout_width以使其他布局属性生效。

因此,将这两行添加到您的第二个视图中:

android:layout_width="0dp"
android:layout_height="0dp"
Run Code Online (Sandbox Code Playgroud)

(注意 for ConstraintLayout,0dp表示匹配约束)


Raj*_*shi 6

用这个方法。您缺少中间布局的布局高度和宽度。

<include
    android:id="@+id/lay1"
    layout="@layout/empty_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<include
    android:id="@+id/lay2"
    layout="@layout/empty_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@+id/lay1"
     app:layout_constraintBottom_toTopOf="@id/lay3"/>

<include
    android:id="@+id/lay3"
    layout="@layout/empty_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    app:layout_constraintBottom_toBottomOf="parent" />
Run Code Online (Sandbox Code Playgroud)