CardView的学习记录

来源:互联网 发布:表白源码 编辑:程序博客网 时间:2024/06/08 02:03

Android5.0增加了一些新的控件,不过说实话CardView还真没咋用过呢,趁着休息学习一下CardView,翻译过来就是卡片,看了一下这个View的特有属性很少,它继承自FrameLayout,能为我们提供带圆角的ViewGroup容器,免去了我们自己需要生成shape的烦恼。

使用步骤

首先需要添加V7包依赖:compile ‘com.android.support:cardview-v7:25.3.1’

在布局中使用它:

<android.support.v7.widget.CardView    android:layout_width="match_parent"    android:layout_height="100dp"    android:layout_centerInParent="true"    app:cardBackgroundColor="#ffd300"    app:cardCornerRadius="30dp"   ></android.support.v7.widget.CardView>

基本效果是这样的:

在CardView里面添加一些子View:

` <TextView        android:layout_width="match_parent"        android:layout_height="40dp"        android:background="#ff0000"        android:gravity="center"        android:text="测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据" />    <TextView        android:layout_width="match_parent"        android:layout_height="60dp"        android:layout_marginTop="40dp"        android:background="#ff00ff"        android:gravity="center"        android:text="也是测试数据" />`

效果图是这样的:

当然了CardView还有几种属性可以设置,可以尝试一下:

<!--app:cardUseCompatPadding="true"--><!-- app:cardMaxElevation="20dp"--><!--app:cardPreventCornerOverlap="false"--><!-- app:contentPaddingLeft/Right/Top/Bottom="20dp"--><!--app:cardElevation="10dp" app:contentPadding="10dp"-->