CardView提供了一个默认的elevation(以为CardView的Z轴阴影)和圆角角度
首先我们需要在res/values/attrs中定义属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CardView">
<attr name="cardBackgroundColor" format="color" />背景色 <attr name="cardCornerRadius" format="dimension" />圆角度 <attr name="cardElevation" format="dimension" />阴影的高度 <attr name="contentPadding" format="dimension" />边界距离内部的距离 <attr name="contentPaddingLeft" format="dimension" /> <attr name="contentPaddingRight" format="dimension" /> <attr name="contentPaddingTop" format="dimension" /> <attr name="contentPaddingBottom" format="dimension" /> </declare-styleable> </resources>
布局文件:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:CardView="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/cardView" android:layout_width="500dp" android:layout_height="500dp" android:layout_gravity="center" android:layout_margin="80dp" CardView:cardCornerRadius="14dp" CardView:cardElevation="8dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="TextView in CardView" android:textColor="#000000" android:textSize="26sp" /> </android.support.v7.widget.CardView>我们还需要在build.grade文件中配置compile 'com.android.support:cardview-v7:21.0.+'
配置cardView的步骤如下:file--Project Structure--选中当前module的名称
然后点击右边的dependencies--点击窗口下方的➕号然后选择library dependency--
然后选择com.android.support:cardview-v7:21.0.+点击ok即可完成cardview的配置
效果图:
![]()