Android,ConstraintLayout:在保持约束的情况下一个接一个放置视图

时间:2018-10-24 09:37:12

标签: android listview android-constraintlayout

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

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

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

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

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

有人可以帮我吗?如有必要,我还可以将constraintLayout更改为其他内容

2 个答案:

答案 0 :(得分:1)

如果您希望View1View2停留在ListView的底部,即使该列表很短并且没有填充整个剩余空间,那么{{1} }高度必须设置为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属性。

答案 1 :(得分:0)

尝试在您的约束布局内创建一个relativeLayout