本文主要介绍CardView的使用,CardView是继承自FrameLayout,
使用比较简单,只需要用CardView包含其他View就可以实现卡片效果了。
加入依赖库
compile 'com.android.support:cardview-v7:22.2.0'
在布局中使用
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" app:cardElevation="@dimen/cardview_compat_inset_shadow" app:cardCornerRadius="@dimen/cardview_default_radius" app:cardMaxElevation="@dimen/cardview_default_elevation" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="20dp"> <TextView android:id="@+id/textview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/trytry" android:textColor="@android:color/holo_blue_dark" android:textSize="25dp" /> <TextView android:id="@+id/desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="好多东西" android:textColor="@android:color/holo_green_dark" /> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout>
其中
- app:cardBackgroundColor 设置CardView背景颜色
- app:cardCornerRadius 设置CardView圆角大小
- app:cardElevation 设置CardView阴影高度
运行效果: