约束布局不约束其他布局的嵌套元素

Constraint Layout does'nt constraint nested elements of other layouts

我正在尝试创建一个列表布局,它从按钮底部开始,占屏幕宽度的 3/4。

就像这样:

首先,我使用了水平和垂直的嵌套线性布局,但后来我读到 here 约束布局的响应速度更快,给我留下了深刻的印象并尝试了一下。

所以,我尝试将外部布局替换为约束布局,但现在,我无法将列表约束为从按钮的 底部 开始,它开始于屏幕顶部。 我试图看看如果我用一个按钮替换整个线性布局会发生什么,只是为了查看属性是否正确实现并且它有效,所以我明白约束布局不会影响线性布局的儿子,尽管它应该影响整个布局(如我所见)。

我的代码如下:

<?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"
    tools:context=".MainActivity">


    <ImageButton
        android:id="@+id/settings_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="#FFFFFF"
        android:src="@drawable/ic_menu_black_32dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <LinearLayout
        android:id="@+id/list_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/settings_button"
        app:layout_constraintTop_toBottomOf="@+id/settings_button">

        <ListView
            android:id="@+id/listview"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

我当前的布局是这样的:

真的很感谢各位帮手!

我认为,使用 match_parent 作为 child 的宽度或高度并添加任何约束在这里是错误的。 已编辑 xml(如果需要可更改页边距):

<?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"
tools:context=".MainActivity">


<ImageButton
    android:id="@+id/settings_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="24dp"
    android:backgroundTint="#FFFFFF"
    android:src="@drawable/ic_menu_black_32dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<LinearLayout
    android:id="@+id/list_linear_layout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="24dp"
    app:layout_constrainedWidth="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="@+id/settings_button"
    app:layout_constraintTop_toBottomOf="@+id/settings_button"
    app:layout_constraintWidth_percent="0.75">

    <ListView
        android:id="@+id/listview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"/>
</LinearLayout>

你需要把线性布局的布局高度改成0dp,我给你放在这里。也考虑迁移到 androidx :)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">


<ImageButton
    android:id="@+id/settings_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="#FFFFFF"
    android:src="@drawable/ic_menu_black_32dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<LinearLayout
    android:id="@+id/list_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:weightSum="4"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/settings_button"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/settings_button">

    <ListView
        android:id="@+id/listview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>