本文介绍: 一、CardView顾名思义,就是卡片布局。现在卡片布局越来越普及了,以前都是各种shape实现卡片效果,现在可以直接控件实现,是不是美滋滋?CardView实用性还是比较强的,一起来试试吧?1.1 导入material库要求SDK大于等于34,如果不打算兼容那么高的,建议降低至1.8.0,不影响CardView的使用。CardView继承自FrameLayout可以我们使用类似卡片布局来显示一致性效果内容。同时卡片可以包含圆角阴影效果。1.2 属性

目录

一、CardView

1.1 导入material库

1.2 属性

二、使用(效果)

2.1 圆角卡片效果


        顾名思义,就是卡片布局。现在卡片布局越来越普及了,以前都是各种shape来实现卡片效果,现在可以直接用控件来实现,是不是美滋滋?CardView实用性还是较强的,一起来试试吧?

implementation 'com.google.android.material:material:1.10.0'

material:1.10.0
要求SDK大于等于34,如果不打算兼容那么高的,建议降低至
1.8.0
,不影响
CardView
使用

        CardView继承自FrameLayout可以让我们使用类似卡片布局来显示一致性效果内容。同时卡片可以包含圆角阴影效果

需要注意的是这里前缀
app
,而不是
android

        默认是带点阴影效果的。

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:background="@color/purple_200"
        app:cardCornerRadius="10dp"
        app:contentPadding="20dp"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:text="圆角 CardView "
            android:textColor="@color/black"
            android:layout_height="wrap_content"/>
    </androidx.cardview.widget.CardView>

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        app:cardCornerRadius="10dp"
        app:cardElevation="20dp"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="20dp"
            android:text="阴影 CardView"
            android:textColor="@color/black"
            android:layout_height="wrap_content"/>
    </androidx.cardview.widget.CardView>
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        app:cardBackgroundColor="@color/purple_200"
        app:cardCornerRadius="10dp"
        app:cardElevation="20dp"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">
            <TextView
                .../>
            <TextView
                .../>
        </LinearLayout>
    </androidx.cardview.widget.CardView>
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        app:cardCornerRadius="10dp"
        app:cardElevation="20dp"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:background="@color/purple_200"
            android:layout_height="wrap_content">
            <TextView
                .../>
            <TextView
                .../>
        </LinearLayout>
    </androidx.cardview.widget.CardView>

        同时设置 app:cardBackgroundColorandroid:background

  <androidx.cardview.widget.CardView
        app:cardBackgroundColor="#44ff0000"
        android:background="#440000ff"
        ...>
        <TextView
            android:background="#440000ff"
            />
    </androidx.cardview.widget.CardView>
    <androidx.cardview.widget.CardView
        ...
        app:cardBackgroundColor="#ff0000"
        android:background="#00ff00">
        <TextView
            ...
            android:background="#0000ff"/>
    </androidx.cardview.widget.CardView>

android:background明显会被app:cardBackgroundColor消费(覆盖)掉

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注