简体   繁体   中英

How to create 3 generic card views?

I'm trying to create 3 cardviews, each have the same height, weight etc..

I managed to create one:

    <?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>

I need two more of this cardview, but I'm getting

"multiple root tag" error

Do I have to create a base layout like Relative layout for all cardviews?

Try to make your cardView element inside a LinearLayout or some other Layout views. example:

   <?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"  .......

You need a root view to put inside your CardViews. Right now you are putting one CardView inside the other (Because your root is a CardView). Try to put them inside a 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM