【问题标题】:Constraint Layout does'nt constraint nested elements of other layouts约束布局不约束其他布局的嵌套元素
【发布时间】:2019-03-29 05:41:06
【问题描述】:

我正在尝试创建从按钮底部开始的列表布局,它是屏幕宽度的 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>

我当前的布局如下所示:

我真的很感谢帮助者!

【问题讨论】:

  • 您好,您能否将屏幕截图添加到您的布局中?
  • 已添加! @Traabefi

标签: android android-linearlayout android-constraintlayout


【解决方案1】:

你需要把线性布局的布局高度改为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>

【讨论】:

    【解决方案2】:

    我认为,在这里使用 match_parent 作为孩子的宽度或高度并添加任何约束是错误的。 已编辑 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>
    

    【讨论】:

    • 你真是太棒了! app:layout_constraintWidth_percent="0.75" 实际上也节省了第二个线性布局的使用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2022-11-02
    • 2020-06-29
    相关资源
    最近更新 更多