Android组件之CardView的基本使用

来源:互联网 发布:手机淘宝如何投诉盗图 编辑:程序博客网 时间:2024/05/18 16:37

先上效果图(这里我结合了RecyclerView使用,有关RecyclerView的基本用法请看这里)

这里写图片描述

CardView是v7包中的一个组件,颜值不错。而且根据谷歌的Material Design,CardView也是正房的地位。
CardView经常在ListView和RecyclerView的Item布局中作为容器使用。

1.导入依赖包

在build.gradle(Module:app)的dependencies中添加:

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

2.在XML中使用

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="wrap_content"    <--!可点击-->    android:clickable="true"    <--!设置水纹点击效果,在旧版本则是一个变深/变亮的效果-->    android:foreground="?attr/selectableItemBackground"    <--!设置圆角-->    app:cardCornerRadius="2dp"    <--!设置高度-->    app:cardElevation="5dp"    app:contentPadding="10dp"    app:cardUseCompatPadding="true"    app:cardPreventCornerOverlap="true">    <TextView        android:layout_width="match_parent"        android:layout_height="80dp"        android:id="@+id/textView"/></android.support.v7.widget.CardView>

基本属性

属性 作用 app:cardBackgroundColor 设置背景颜色 app:cardCornerRadius 设置圆角大小 app:cardElevation 设置z轴的阴影 app:cardMaxElevation 设置z轴的最大高度值 app:cardUseCompatPadding 是否使用CompatPadding(作用似乎是自动设置CardView的margin等属性) app:cardPreventCornerOverlap 是否使用PreventCornerOverlap()(使内容与圆角不重叠) app:contentPadding 设置内容的padding app:contentPaddingLeft 设置内容的左padding app:contentPaddingTop 设置内容的上padding app:contentPaddingRight 设置内容的右padding app:contentPaddingBottom 设置内容的底padding

总的来说,CardView使用还是很简单的,而且也是非常美观。你值得拥有。

源码在这里

参考:http://www.jianshu.com/p/33b1d21d6ba6
http://blog.csdn.net/javacainiao931121/article/details/51720807

0 0