首页 > 解决方案 > Android,ConstraintLayout:在保持约束的同时一个接一个地定位视图

问题描述

我正在努力寻找解决方案。

基本上,我已经有了一个带有页眉和页脚的布局。我需要将 1 个 ListView 和 2 个 Views 放在剩余空间内,以便:

我尝试了所有可能的链组合,将视图放在线性布局中,仅使用约束和偏差,但我得到的最接近的方法是以正确的顺序打包视图但居中而不是在屏幕顶部。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"/>

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/view1"
        app:layout_constraintTop_toBottomOf="@+id/header"
        app:layout_constraintVertical_chainStyle="packed" />

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/view2"        
        app:layout_constraintTop_toBottomOf="@+id/list_view" />

    <View
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/footer"
        app:layout_constraintTop_toBottomOf="@+id/view1"/>


    <View
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

有人可以帮我吗?如有必要,我也可以将约束布局更改为其他内容

标签: androidlistviewandroid-constraintlayout

解决方案


如果您希望View1andView2坚持到底部,ListView即使列表很短并且没有填满整个剩余空间,那么ListView's高度必须设置为wrap_content。要保持三个小部件与顶部对齐,您可以创建一个垂直链,其样式packed和垂直偏差为0. 这是一个示例布局,TextViews用于展示这个想法:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        tools:text="Header"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constrainedHeight="true"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintVertical_bias="0"
        app:layout_constraintBottom_toTopOf="@id/view1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/header" />

    <TextView
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/view2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/listview"
        tools:text="View 1"/>

    <TextView
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/footer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/view1"
        tools:text="View 2"/>

    <TextView
        android:id="@+id/footer"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        tools:text="Footer"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

如果您想ListView填充剩余空间而不管其内容如何,​​只需将其高度更改为0dp并删除app:layout_constrainedHeight="true"属性即可。


推荐阅读