如何解决在相对的两边对齐两个视图一个在顶部,一个在底部,并能够在顶视图增大时推动底视图?
我基本上在视图的顶部对齐了EditText,在视图的底部也有一个RecyclerView,它也可以增长(最新的项在底部) 使用约束布局很容易做到这一点,但是我的问题是,当EditText增长时,它应该开始向下推列表。但是该列表最初在父级的底部对齐。 (并且所有内容都应可滚动) 我希望这张图片能让事情更清晰
解决方法
这里的技巧是避免形成垂直链(以便始终将顶部的EditText留在原处),并利用RecyclerView上的app:layout_constrainedHeight
属性,以使其在EditText增大时缩小
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<EditText
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/text"
app:layout_constraintVertical_bias="1"/>
</androidx.constraintlayout.widget.ConstraintLayout>
android:layout_height="wrap_content"
和app:layout_constrainedHeight="true"
的组合,以及顶部和底部约束,意味着RecyclerView始终仅与项目或一样高在EditText下方,以较小者为准。
app:layout_constraintVertical_bias="1"
属性可确保当列表没有填满屏幕时,它位于底部。
我将添加到上一个答案,如果您希望能够滚动布局,则可以使用以下命令:
<androidx.core.widget.NestedScrollView 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"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit_text"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
这将使所有内容保持一致,并且可以向下滚动以查看完整的recyclerView
。 NestedScrollView
将确保一切顺利进行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。