【问题标题】:Android, ConstraintLayout: position views one after another while maintaining constraintsAndroid,ConstraintLayout:在保持约束的同时一个接一个地定位视图
【发布时间】:2018-10-24 18:10:52
【问题描述】:

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

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

  • ListView 从顶部开始并填满所有可用空间
  • View1 就在 ListView 之后
  • View2 在 View1 之后
  • 如果 ListView 不可见(或很短),则应显示视图 位于屏幕顶部。
  • ListView 应该扩展到 screen_heigth -header -footer -view1 -view2

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

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

谁能帮我解决这个问题?如有必要,我也可以将约束布局更改为其他内容

【问题讨论】:

    标签: android listview android-constraintlayout


    【解决方案1】:

    如果您希望View1View2 粘在ListView 的底部,即使列表很短并且没有填满整个剩余空间,那么ListView's 的高度必须设置为@ 987654325@。要使三个小部件与顶部对齐,您可以创建一个样式为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" 属性。

    【讨论】:

      【解决方案2】:

      尝试在你的约束布局中创建一个 relativeLayout

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        相关资源
        最近更新 更多