Android,约束布局:在保持约束的同时,一个接一个地定位视图



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

基本上,我有一个布局,里面已经有一个页眉和一个页脚。我需要在剩余空间内放置 1 个列表视图和 2 个视图,以便:

  • 列表视图从顶部开始并填充所有可用空间
  • View1 紧跟在列表视图之后
  • 视图
  • 2 紧随视图 1 之后
  • 如果列表视图不可见(或很短(,则应显示视图 在屏幕顶部。
  • 列表视图应展开为 screen_heigth -页眉 -页脚 -视图 1 -视图 2

我尝试了所有可能的链式组合,将视图放在 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>

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

如果您希望View1View2粘在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"属性即可。

尝试在约束布局中创建一个相对布局

最新更新