CardView个人总结

本文介绍了Android中CardView组件的基本用法及配置属性。CardView是Android 5.0引入的一个展示卡片效果的布局控件,它继承自FrameLayout,支持设置阴影、圆角等特性。文中通过实例展示了如何使用CardView创建不同样式的卡片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

Android5提供的,在v7包里面

Gradle依赖

compile 'com.android.support:cardview-v7:23.2.0'

特点

Cardview继承自FrameLayout,所以子控件布局规则和FrameLayout的一样

常用属性

card_view:cardElevation         阴影的大小

card_view:cardMaxElevation     阴影最大高度

card_view:cardBackgroundColor 卡片的背景色

card_view:cardCornerRadius     卡片的圆角大小

card_view:contentPadding     卡片内容于边距的间隔

card_view:contentPaddingBottom 卡片内容与底部的边距

card_view:contentPaddingTop 卡片内容与顶部的边距

card_view:contentPaddingLeft 卡片内容与左边的边距

card_view:contentPaddingRight 卡片内容与右边的边距

card_view:contentPaddingStart 卡片内容于边距的间隔起始

card_view:contentPaddingEnd 卡片内容于边距的间隔终止

card_view:cardUseCompatPadding 设置内边距,V21+的版本和之前的版本仍旧具有一样的计算方式

card_view:cardPreventConrerOverlap V20和之前的版本中添加内边距,这个属性为了防止内容和边角的重叠


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <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="100dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:clickable="true"
        card_view:cardCornerRadius="10dp"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardElevation="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="圆角矩形" />

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

    <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_marginTop="50dp"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardElevation="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="矩形且填充父容器" />

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

    <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="100dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="50dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardElevation="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="歌唱祖国3" />

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


</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值