Android RecyclerView底部添加按钮,按钮与屏幕同宽
Android RecyclerView底部添加按钮,按钮与屏幕同宽
在Android开发中,我们经常需要在RecyclerView的底部添加一个按钮,并且希望按钮能够与屏幕宽度一致。
以下布局代码使用ConstraintLayout实现了这个效果:
<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'>
<!-- 条目列表 -->
<androidx.recyclerview.widget.RecyclerView
android:id='@+id/recyclerView'
android:layout_width='match_parent'
android:layout_height='0dp'
app:layout_constraintTop_toTopOf='parent'
app:layout_constraintBottom_toTopOf='@id/addButton' />
<!-- +号按键 -->
<ImageButton
android:id='@+id/addButton'
android:layout_width='match_parent'
android:layout_height='wrap_content'
android:src='@drawable/ic_add'
app:layout_constraintBottom_toBottomOf='parent'/>
</androidx.constraintlayout.widget.ConstraintLayout>
代码解释:
-
RecyclerView:
android:layout_height='0dp':将RecyclerView的高度设置为0dp,使其能够填充ConstraintLayout中剩余的空间。app:layout_constraintBottom_toTopOf='@id/addButton':将RecyclerView的底部约束设置为按钮的顶部,确保按钮始终位于RecyclerView的底部。
-
ImageButton:
android:layout_width='match_parent':将按钮的宽度设置为match_parent,使其与屏幕宽度一致。app:layout_constraintBottom_toBottomOf='parent':将按钮的底部约束设置为父布局的底部,确保按钮始终位于屏幕底部。
通过以上布局代码,我们就能够实现RecyclerView底部添加一个与屏幕同宽的按钮的效果。
原文地址: https://www.cveoy.top/t/topic/RZo 著作权归作者所有。请勿转载和采集!