android - LinearLayout 水平方向不适用于 RecycleView 元素
问题描述
我尝试在蚀刻线上将所有回收视图元素对齐 2,但它们仅垂直对齐线上的一个元素。
这是我的代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:id="@+id/card_pdf"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:layout_weight="1">
<TextView
android:id="@+id/tv_pdf_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ceva"
android:textSize="25sp"
android:padding="6dp"/>
</androidx.cardview.widget.CardView>
</LinearLayout>
解决方案
您似乎不了解 RecyclerView 的概念或概念。一个 ViewHolder 仅包含 1 个项目(因此您创建的布局仅影响一个 ViewHolder,而不影响整个 RecyclerView,您将永远无法实现您想要的)。
相反,将这些行添加到 xml 中的您的 RecyclerView:
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
GridLayoutManager
在网格中布置项目。spanCount
定义该网格中有多少列。
您的项目布局中也不需要 a LinearLayout
。你应该只有CardView
和TextView
。
推荐阅读
- c - 读者 作家 - 谁得到偏好
- css-in-js - 如何使用 CSS-in-JS 定位元素?
- python-3.x - Pyautogui如何单击单选按钮
- sql-server - SSIS:执行 SQL 任务与数据流任务
- swiftui - 需要确认在 iOS14 中被双重加载的视图
- jmeter - 非 GUI 模式下 Jmeter 测试后生成 .jtl(用于分析)和 .csv(用于报告)文件
- c# - Umbraco 8 模型生成器无法找到 doctype 生成的文件
- python - 如何使用击键将 python 复制并粘贴到某个应用程序中?
- java - Ebean RawSql 不允许在“case when”子句中使用命名参数
- java - Androd Studio 在特定时间安排一个方法