CardView使用与介绍

1.在Android Studio工程中build.gradle中配置如下:
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    // 引用android.support.v7.CardView包
    compile 'com.android.support:cardview-v7:23.+'
}
2.CardView分析

首先浏览一下CardView源码,在CardView源码中这样介绍:

A FrameLayout with a rounded corner background and shadow(一个带圆角和阴影的布局)

public class CardView extends FrameLayout {
    ......
}

一般我们经常实现带圆角的View时都会首先想到写一个shape来实现效果,但CardView就自带了这种属性。

3.CardView属性介绍, 如下:
  • CardView_cardBackgroundColor 设置背景色
  • CardView_cardCornerRadius 设置圆角大小
  • CardView_cardElevation 设置z轴阴影
  • CardView_cardMaxElevation 设置z轴最大高度值
  • CardView_cardUseCompatPadding 是否使用CompadPadding
  • CardView_cardPreventCornerOverlap 是否使用PreventCornerOverlap
  • CardView_contentPadding 内容的padding
  • CardView_contentPaddingLeft 内容的左padding
  • CardView_contentPaddingTop 内容的上padding
  • CardView_contentPaddingRight 内容的右padding
  • CardView_contentPaddingBottom 内容的底padding
  • CardView_cardUseCompatPadding 设置内边距
  • CardView_cardPreventConrerOverlap 防止内容和边角的重叠
4.具体使用:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="#FFFFFF"
    card_view:cardCornerRadius="10dp">

    <!- 要展示在CardView上的子View ->
    ......

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

注意:使用CardView时注意”命名空间”

xmlns:card_view="http://schemas.android.com/apk/res-auto"

这句坚决不能少,否则CardView那些自定义属性就没法使用了。

5.几种效果图

CardView效果图
代码如下:

<android.support.v7.widget.CardView    
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card:cardBackgroundColor="#7FFFD4"
    card:cardCornerRadius="10dp"
    card:cardElevation="5dp"
    card:cardUseCompatPadding="true">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="30dp"
        android:text="设置z轴阴影10dp"
        android:textColor="#FFFFFF" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card:cardBackgroundColor="#ffd700"
    card:cardCornerRadius="10dp"
    card:cardUseCompatPadding="true">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="30dp"
        android:text="设置圆角10dp"
        android:textColor="#FFFFFF" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="30dp"
        android:text="正常效果" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    card:cardBackgroundColor="#ff00ff"
    card:cardUseCompatPadding="true">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="30dp"
        android:text="设置背景"
        android:textColor="#FFFFFF" />
</android.support.v7.widget.CardView>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蒙同學

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值