ym—— 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

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/textViewSample"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20sp"
        android:padding="30dp"
        android:fontFamily="sans-serif-light"
        tools:text="Sample text"
        />

</LinearLayout>
修改后的xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp" >

    <TextView
        android:id="@+id/textViewSample"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fontFamily="sans-serif-light"
        android:padding="30dp"
        android:textSize="20sp" />

</android.support.v7.widget.CardView>
哦了~!赶快去试试吧~!


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值