首页 > 解决方案 > 并排显示项目

问题描述

我正在尝试使用 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>

标签: androidandroid-layoutkotlin

解决方案


将您的根线性更改layout_widthwrap_content. 然后当你设置回收器时,将 layoutmanager 更改为 GridLayoutManager,例如:

int columnSize = 3;
mLayoutManager = new GridLayoutManager(this, columnSize);
mRecyclerView.setLayoutManager(mLayoutManager);

文档链接


推荐阅读