CardView的使用
CardView是在安卓5.0引入的卡片式控件。
不使用CardView效果:
使用CardView效果:
一、导包
compile 'com.android.support:cardview-v7:25.3.1'
二、xml添加控件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardBackgroundColor="@color/theme_color"
app:cardCornerRadius="20dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
app:contentPadding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/user_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/user_head"
android:layout_centerVertical="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
CardView属性说明:
- app:cardBackgroundColor:设置背景颜色。
- app:cardCornerRadius:设置圆角大小。
- app:cardElevation:设置阴影的高度。
- app:cardMaxElevation:设置阴影的最大高度值。
- app:cardUseCompatPadding:是否设置UseCompatPadding。v21+的版本和之前的版本仍旧具有一样的计算方式。此为卡片与父容器的边距。
- app:cardPreventCornerOverlap:是否设置PreventCornerOverlap。在v20和之前的版本中添加内边距,这个属性是为了防止卡片内容和边角的重叠。此为卡片与父容器的边距。
- app:contentPadding:设置内容的padding。此为卡片与内容的边距。
- app:contentPaddingLeft:设置内容的左padding。此为卡片与内容的边距。
- app:contentPaddingTop:设置内容的上padding。 此为卡片与内容的边距。
- app:contentPaddingRight: 设置内容的右padding。此为卡片与内容的边距。
- app:contentPaddingBottom:设置内容的底padding。此为卡片与内容的边距。