You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何不使用RecyclerView向CardView中添加子项?

How to Populate a Single CardView with Image-Text Rows (No RecyclerView Needed)

I totally get it—most tutorials fixate on RecyclerView when dealing with item lists, but for a single CardView, you can keep things simple by building the layout directly inside it. Here’s a straightforward, step-by-step solution to create the exact structure you want:


1. XML Layout Setup

Start with your empty CardView, then nest a vertical container (like LinearLayout or ConstraintLayout) inside it to hold each image-text row. Below is a complete example using LinearLayout for simplicity:

<androidx.cardview.widget.CardView
    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"
    app:cardCornerRadius="8dp"
    app:cardElevation="4dp"
    android:padding="16dp">

    <!-- Vertical container for all rows (adds spacing between items) -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gap="12dp">

        <!-- First row: Image + Text -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:alignItems="center"
            android:gap="8dp">

            <ImageView
                android:id="@+id/img_row1"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:src="@drawable/your_image_1"
                android:scaleType="centerCrop"/>

            <TextView
                android:id="@+id/txt_row1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="text here"
                android:textSize="16sp"/>
        </LinearLayout>

        <!-- Second row: Image + Text -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:alignItems="center"
            android:gap="8dp">

            <ImageView
                android:id="@+id/img_row2"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:src="@drawable/your_image_2"
                android:scaleType="centerCrop"/>

            <TextView
                android:id="@+id/txt_row2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="text here"
                android:textSize="16sp"/>
        </LinearLayout>

        <!-- Third row: Only Image -->
        <ImageView
            android:id="@+id/img_row3"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:src="@drawable/your_image_3"
            android:scaleType="centerCrop"/>

    </LinearLayout>

</androidx.cardview.widget.CardView>

Key Notes:

  • The outer CardView includes basic styling (corner radius, elevation, padding) for that standard card appearance.
  • The vertical LinearLayout acts as a container for all rows, using gap to add clean spacing between items.
  • Each image-text row uses a horizontal LinearLayout to align elements side by side, centered vertically.

Alternative: ConstraintLayout for Precise Control

If you want the text to fill the remaining space next to the image, swap the horizontal LinearLayouts with ConstraintLayout:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="48dp">

    <ImageView
        android:id="@+id/img_row1"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/your_image_1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/txt_row1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="text here"
        android:textSize="16sp"
        app:layout_constraintStart_toEndOf="@id/img_row1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:marginStart="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>

2. Dynamic Content (Optional)

If you need to set images or text programmatically (instead of hardcoding in XML), reference the views by their IDs and update them:

Kotlin Example

// Inside your Activity/Fragment
val imgRow1 = findViewById<ImageView>(R.id.img_row1)
val txtRow1 = findViewById<TextView>(R.id.txt_row1)

imgRow1.setImageResource(R.drawable.dynamic_image_1)
txtRow1.text = "Dynamic text for row 1"

// Repeat for other rows as needed

Java Example

// Inside your Activity/Fragment
ImageView imgRow1 = findViewById(R.id.img_row1);
TextView txtRow1 = findViewById(R.id.txt_row1);

imgRow1.setImageResource(R.drawable.dynamic_image_1);
txtRow1.setText("Dynamic text for row 1");

Quick Reminder

RecyclerView is designed for lists of multiple repeated items. For a single CardView, you don’t need adapters or list logic—just build the layout directly inside it using standard Android view groups.

内容的提问来源于stack exchange,提问作者Daniel k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:50:03