首页 > 解决方案 > 使cardview中的两个项目出现在一行中

问题描述

我正在使用 firebase 数据库在回收站视图中列出项目,并且我希望两个项目位于同一行,如下所示: 在此处输入图像描述

但对我来说,它是这样显示的, 在此处输入图像描述 这是我的卡片视图列表布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginTop="20dp"
    android:layout_marginStart="15dp"
    android:layout_marginEnd="15dp"
    app:cardElevation="15dp"
    >


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/product_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Product Name"
            android:textAlignment="center"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/design_default_color_primary_dark"/>
        <ImageView
            android:id="@+id/product_image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_below="@+id/product_name"
            android:scaleType="centerCrop"
            android:layout_marginTop="2dp"/>
        <TextView
            android:id="@+id/product_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Product Price"
            android:textAlignment="center"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_below="@id/product_image"
            android:textColor="@color/design_default_color_primary_dark"/>
        <TextView
            android:id="@+id/product_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Product Description"
            android:textAlignment="center"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginTop="2dp"
            android:layout_below="@id/product_price"
            android:textColor="@color/design_default_color_primary_dark"/>

    </RelativeLayout>


</androidx.cardview.widget.CardView>

我想以某种方式做到这一点,而不是只有一个出现在 recyclerview 的一行中,我希望出现多个

标签: xmlfirebaseresponsive-designandroid-cardview

解决方案


使用 ReccyclerView 的网格布局管理器,将 2 作为第二个参数,用于显示 2 列。

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(mLayoutManager);

推荐阅读