android - 如何将水平 RecyclerView 对齐到底部?
问题描述
如何将水平 RecyclerView 对齐到底部?例如,我有一个看起来像这样的水平RecyclerView
:LinearLayoutManager
但
我希望所有 RecyclerView 项目都与底线对齐,如下所示:
有什么方法可以做到这一点?
UPD 这是我的项目布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="@+id/columnImageView"
android:layout_width="@dimen/column_width"
android:layout_height="match_parent"
android:layout_marginHorizontal="@dimen/spacing_xs"
android:src="@drawable/graph_rectangle_white_b"
android:contentDescription="@null"
tools:layout_height="@dimen/spacing_3xl"
tools:visibility="visible"
tools:src="@drawable/graph_rectangle_blue_a" />
</layout>
这就是我的 RecyclerView 的布局:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<merge 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="wrap_content"
android:background="@color/grey_200">
...
<View
android:id="@+id/line5"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/grey_400"
app:layout_constraintBottom_toBottomOf="@id/main"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/chartRecyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:overScrollMode="never"
android:layout_marginHorizontal="@dimen/spacing_xs"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="@id/line5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_alignBottom="@id/line5"
tools:layout_height="wrap_content"
tools:itemCount="5"
tools:listitem="@layout/chart_column_item"
tools:spanCount="5" />
</merge>
</layout>
解决方案
I had your same problem, and I resolved setting in onBindViewHolder the height of the item programmatically like this:
holder.itemView.layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT)
Note that this works if Views in your ViewHolder have fixed height.
Another solution would be to calculate at runtime the margin_top and apply it to holder.itemView
推荐阅读
- sql - 如果语句包含没有 INTO 子句的 OUTPUT 子句,则 DML 语句不能有任何启用的触发器
- java - 使用 EasyMock 在 junit 测试用例中跳过方法调用?
- authentication - DNN 9 Persona Bar 登录后不可见
- html - iPhone上的页脚超出屏幕
- bash - Bash:回显文件内容以计数器开头
- javascript - 我们如何访问未定义的对象?
- ruby-on-rails - 如何在rails中使用脚手架创建复选框?
- amazon-web-services - 我可以强制 CloudFormation 解析来自 Secrets Manager 的值吗?
- javascript - Javascript AJAX 调用 SpringBoot 控制器并获得 JSON 响应
- sql - 是否有一种明智的方法可以将 csv 导入 postgres,其中一列每行有多个值?