android - 并排显示项目
问题描述
我正在尝试使用 JSON 和 RecyclerView 在我的测试项目中显示项目。代码工作正常,但我的代码中的问题是我逐行而不是彼此相邻地看到图像。我也尝试过表格布局和卡片布局,但它对我不起作用。任何输入都受到高度赞赏。
当前显示
Image1
Image2
Image3
Image4
Image5
预期产出
Image1 Image2 Image3
Image4 Image5
完整代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
android:id="@+id/PersonImage"
android:layout_width="113dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.328"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/PersonName"
android:layout_width="71dp"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginEnd="8dp"
android:text="Name"
android:textStyle="bold|italic"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/PersonImage"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
解决方案
将您的根线性更改layout_width
为wrap_content
. 然后当你设置回收器时,将 layoutmanager 更改为 GridLayoutManager,例如:
int columnSize = 3;
mLayoutManager = new GridLayoutManager(this, columnSize);
mRecyclerView.setLayoutManager(mLayoutManager);
推荐阅读
- php - 无法在同一页面表单提交上获取 PHP 请求
- android - 如何为 SearchView 投射 CustomEditText 而不是 EditText?
- javascript - 尝试使用 JS 对象填充组件但得到“函数作为 React 子无效”?
- asynchronous - 如何使用任务完成源
- c++ - 加载大 OBJ 文件会导致模型损坏?
- r - 机器特定的慢 read_excel 读取时间
- python - 特征提取 NLP
- php - 两次调用函数时未附加Ajax成功数据
- python - 连接字节时出现类型错误
- python - 如何使用 OpenCV 或深度学习从平面图中扫描房间轮廓数据?