android - Android UI - 动态按钮对齐更改
问题描述
我有以下 xml
<ConstraintLayout>
<NestedScrollView>
<ConstraintLayout>
<RecycleView/>
</ConstraintLayout>
</NestedScrollView>
<Button BottomTo_BottomOf = parent/>
</ConstraintLayout>
当我加载屏幕时,我希望按钮粘在屏幕底部。我在recycleview中动态加载一些元素(基于用户操作),一旦recycleview的元素超过屏幕大小,那么我希望按钮对齐方式从屏幕底部更改到内容的末尾,并且一旦用户滚动就可见所有内容
解决方案
将这些属性添加到Button
:
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
将这些属性添加到NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/bottomButtonId" <--Id of bottom button-->
作为参考,请尝试以下代码:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/button6">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
tools:listitem="@layout/imagepost_buttons"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
推荐阅读
- mysql - 从 2 列中获取最后一个不同的值
- apache-spark-sql - 根据 Spark 查询中的列值填充早期记录中的值
- mysql - 使用连接更新时 Mysql 死锁
- unity3d - VS Code 找不到对某些 Unity 类的引用
- linux - SELinux AVC 拒绝 sshd 的“{ dyntransition }”
- laravel - 文件未上传到 Oracle DB
- regex - 正则表达式的形成和否定问题
- ansible - Packer Ansible 供应商正在跳过 Ansible 剧本中的任务
- runtime - 如何在 Colab Pro+ 中使用高内存
- c - 在 32 位架构上的函数中将 64 位值作为参数传递时会发生什么?