安卓约束布局未按预期工作

10

我有一个根标签为

的活动

androidx.constraintlayout.widget.ConstraintLayout

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

    app:layout_constraintTop_toBottomOf="@+id/lay1"
    app:layout_constraintBottom_toTopOf="@+id/lay3"

我不明白为什么中间的元素会占据整个屏幕,而不是从顶部元素的底部开始到底部元素的顶部结束。你能帮我解决这个问题吗?

这个活动:

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

空的线性布局文件: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>
3个回答

17
如果想要在 <include> 布局中覆盖 layout_ 参数,您必须同时指定 layout_widthlayout_height。参考开发者文档

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

因此,请在第二个视图中添加这两行:

android:layout_width="0dp"
android:layout_height="0dp"

(请注意,在ConstraintLayout中,0dp表示与约束匹配)


5
使用这种方式。您遗漏了中间布局的布局高度和宽度。
<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" />

这个起作用了。为什么将视图的高度设置为0dp可以解决这个问题? - M. Azyoksul
如果您不指定,它将占据整个屏幕。 - Rajnish suryavanshi

4

将以下内容添加到您的中间包含布局中:

 <include
    android:id="@+id/lay2"
    layout="@layout/something"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/lay3"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/lay1" />

您缺少了android:layout_widthandroid:layout_height属性。

我不明白为什么我需要那些属性。我已经定义了每个东西应该放在哪里。 - M. Azyoksul
1
你需要这些来覆盖你包含的布局参数,否则你的布局将会遍布整个屏幕。请查看***@Ben P.***的回答。 - Tamir Abutbul

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