创建带有不同信息内容的碎片,Cards 是一种常见的模式。supportV7下的CardView可以帮助你方便的实现,并提供了轮廓和阴影。
CardView继承自FrameLayout并提供了默认的elevation 和 圆角,使得cards在不同的平台间保持一致。
CardView其实是一个FrameLayout,所以使用方式跟FrameLayout相同
首先需要引入依赖 compile ‘com.android.support:cardview-v7:23.0.0’
在XMl中的属性:
<android.support.v7.widget.CardView
android:id="@+id/view" //控件的ID
android:layout_width="200dp" //控件的宽
android:layout_height="wrap_content" //控件的高
android:layout_centerHorizontal="true" // 水平方向居中
app:cardBackgroundColor="#303069" //CardView的背景色
app:cardElevation="2dp" //阴影面高度
app:cardMaxElevation="2dp" //最大阴影面高度
app:cardCornerRadius="8dp" //CardView的边缘弧度数
app:cardPreventCornerOverlap="true" //防止卡片内容和边角的重叠
app:cardUseCompatPadding="true" //CardView的内边据
app:contentPadding="10dp" //CardView卡片边界距离内部的距离
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你是我的小小苹果"
android:textColor="#fff"/>
</android.support.v7.widget.CardView>