Android 5.0学习之CardView



转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持!


前言

CardView顾名思义,就是想卡片一样的控件,如图


Android 5.0之前,我们有两种方案做出这种效果:

1.通过设置背景图

2.设置配置Shape文件

而现在我们需要麻烦美工MM,也不需要配置麻烦的Shape文件,只需要简单的设置几个属性即可,那就是用我们CardView

CardView

CardView继承了FrameLayout类,并让你在里面的卡片中(显示)有跨平台一致性的外观。CardView控件可以有阴影和圆角(效果)。

要创建具有阴影效果的卡片,可以使用card_view:cardElevation属性。CardView会在Android5.0(API级别21)以上的系统中使用真实高程(elevation)和动态阴影,(而)在较低的系统版本中会回落到程序式的阴影效果显示。欲了解更多信息,请参阅Maintaining Compatibility(保持兼容性)

使用这些属性来定制CardView控件的外观:

在布局中设置圆角半径,使用card_view:cardCornerRadius属性

在代码中设置圆角半径,使用CardView.setRadius方法

要设置一个卡片的背景颜色,使用card_view:cardBackgroundColor属性


为了让大家更好理解并使用本人所讲的知识点,本次CardView作为item添加到RecyclerView中。


使用步骤:

1.导入/sdk/extras/android/support/v7/cardview/libs/android-support-v7-cardview.jar 

2.把/sdk/extras/android/support/v7/cardview作为Library工程引用到工程

3.修改下item的layout

原有的xml

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     xmlns:tools="http://schemas.android.com/tools"  
  6.   
  7.     android:orientation="horizontal"  
  8.     android:layout_width="match_parent"  
  9.     android:layout_height="match_parent"  
  10.     >  
  11.   
  12.     <TextView  
  13.         android:id="@+id/textViewSample"  
  14.         android:layout_width="0dp"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_weight="1"  
  17.         android:textSize="20sp"  
  18.         android:padding="30dp"  
  19.         android:fontFamily="sans-serif-light"  
  20.         tools:text="Sample text"  
  21.         />  
  22.   
  23. </LinearLayout>  
修改后的xml

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:card_view="http://schemas.android.com/apk/res-auto"  
  4.     android:id="@+id/card_view"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:layout_margin="5dp" >  
  8.   
  9.     <TextView  
  10.         android:id="@+id/textViewSample"  
  11.         android:layout_width="0dp"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_weight="1"  
  14.         android:fontFamily="sans-serif-light"  
  15.         android:padding="30dp"  
  16.         android:textSize="20sp" />  
  17.   
  18. </android.support.v7.widget.CardView>  
哦了~!赶快去试试吧~!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值