android - Android,ConstraintLayout:在保持约束的同时一个接一个地定位视图
问题描述
我正在努力寻找解决方案。
基本上,我已经有了一个带有页眉和页脚的布局。我需要将 1 个 ListView 和 2 个 Views 放在剩余空间内,以便:
- 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>
有人可以帮我吗?如有必要,我也可以将约束布局更改为其他内容
解决方案
如果您希望View1
andView2
坚持到底部,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"
属性即可。
推荐阅读
- python - 如何通过原生 API 在交易站显示买单
- javascript - 试图将 id 的值传递给占位符
- azure - Azure API 管理 - 动态 URL 问题
- javascript - Webpack 中的 SVG URL:模块解析失败:意外令牌 (1:0)
- python - 如何计算 num_fft 值?
- mysql - 如何在续集nodejs中从关联模型中加载没有嵌套对象的属性
- javascript - angular js多文件上传无法使用ngfile上传
- django - 当 proxy_set_header 设置为 $host 时 NGINX 不起作用
- regex - 非捕获组仍在选择元逃生
- java - 给定一组数据,检查它是否是二叉搜索树?