如何创建 3 个通用卡片视图?

How to create 3 generic card views?

我正在尝试创建 3 个卡片视图,每个都有相同的高度、重量等。

我成功创建了一个:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_margin="8dp"
    android:padding="8dp">

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:scaleType="centerInside"
        android:src="@drawable/moon20"
        android:background="@android:color/white"
        android:padding="8dp"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="3"
        android:padding="8dp"
        android:text="20 min Power Nap"
        android:textColor="@color/colorSecondaryText"
        android:textStyle="bold"
        android:textSize="20dp"
        android:textAlignment="center"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        />


  </RelativeLayout>
</android.support.v7.widget.CardView>

我还需要两个卡片视图,但我得到了

"multiple root tag" error

我是否必须为所有卡片视图创建类似相对布局的基本布局?

尝试将您的 cardView 元素置于 LinearLayout 或其他一些布局视图中。 示例:

   <?xml version="1.0" encoding="utf-8"?>

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent" >

       <android.support.v7.widget.CardView       
       xmlns:android="http://schemas.android.com/apk/res/android"  .......

您需要一个根视图来放入您的 CardView 中。现在您将一个 CardView 放入另一个(因为您的根目录是一个 CardView)。尝试将它们放在 LinearLayout 中。

<LinearLayout>
     <CardView>
     <CardView>
     <CardView>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >

     <CardView>
     <CardView>
     <CardView>
</LinearLayout>