android - 如何修复约束布局中不必要的空间?
问题描述
我有一个Constraint Layout
. 目标是将两个 s 堆叠recyclerview
在一起。我使用 a 的原因Constraint Layout
是因为我也希望 aTextView
占据与底部相同的空间recyclerview
。TextView
(当底部recyclerview
没有内容时,我将以编程方式将 设置为可见)。
这是布局:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@color/heart_red"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/tabRecyclerView"
android:layout_width="0dp"
android:layout_height="50dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/imageRecyclerView"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/imageRecyclerView"
android:layout_width="0dp"
android:layout_height="200dp"
android:background="@color/lightshade"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@id/tabRecyclerView" />
</android.support.constraint.ConstraintLayout>
此外,这是我的项目列的布局imageRecyclerView
:
<?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="wrap_content"
android:layout_height="match_parent"
android:background="@color/darkshade">
<ImageView
android:id="@+id/itemImage"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:layout_width="wrap_content"
android:layout_height="150dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/itemFavoriteButton"/>
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="@+id/itemProgressBar"
android:indeterminate="true"
android:layout_width="wrap_content"
android:layout_height="150dp"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/itemFavoriteButton"/>
<ToggleButton
android:id="@+id/itemFavoriteButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/ic_favorite"
android:textOff=""
android:textOn=""
android:text=""
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/itemImage"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
但是,由于某种原因,底部有一个红色条Constraint Layout
,如下所示:
据我所知,从数学上讲,一切都已验证。那么为什么我的布局底部有一个红色条?
解决方案
您正试图将回收站视图堆叠在一起,对吗?
目前,您无法将 tabRecyclerView 和 imageRecyclerView 的高度相加,因为它们相互重叠。
看一下这个
父布局高度为 250dp
tabRecyclerView 是 50dp
- imageRecyclerView 是 200dp(但是因为这条线
app:layout_constraintTop_toTopOf="@id/tabRecyclerView"
,它的 tabRecyclerView 重叠) - 缺少 50dp(这就是显示父级的红色背景的原因)
修理
改变
app:layout_constraintTop_toTopOf="@id/tabRecyclerView"
至
app:layout_constraintTop_toBottomOf="@id/tabRecyclerView"
推荐阅读
- python - Postgresql 是否有可能对二进制数据执行立方体功能?
- woocommerce - Woocommerce - 如何在结帐视图上显示运输方式(并更改它们)?
- flutter - 为什么没有 ValueNotifierBuilder?
- c# - 寻找增强算法:保存游戏状态
- angular - 如何组织更好的验证流程?(使用自定义验证器控制值访问器)
- c++ - 如何通过 generate_n 以索引增量填充 STL 容器
- html - FF 响应式设计模式选择下拉菜单
- tensorflow - 错误运行 tensorflow lite 示例
- javascript - If 语句检查表是否包含在 iframe 中
- codenameone - 嵌套在可滚动容器中的行为良好的自定义开关或滑块组件