ANDROID L——RecyclerView,CardView导入和使用(Demo)


  4491人阅读  评论(0)  收藏

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


简介:


这篇文章是ANDROID L——Material Design详解(UI控件)的一个补充或者说是应用实例,如果有时间建议大家稍微浏览一下上篇文章。


本文主要介绍Android L新增加的两个UI控件RecyclerViewCardView的导入和使用。

RecyclerView是ListView的升级版

CardView则是Google提供的一个卡片式视图组件


本例就是使用RecyclerView来展示多个CardView的一个小例子,先看下效果图:






导入RecyclerView,CardView


由于RecyclerView,CardView是放在support library v7包中,所以我们想要使用就必须要导包。

下面就介绍下在EclipseAndroid Studio中是如何导入这两个包的。



Eclipse:


第一步:通过SDK manager下载/更新Android Support Libraries(5.0版本最新为21) 



第二步:导入CardView和RecyclerView项目(都在support v7中)

1.在Eclipse中点击Import,导入Android项目

2.导入CardView和RecycleView,路径为your sdk path\extras\android\support\v7\cardview(RecycleView则为相同目录下的recyclerview)

3.导入时记得将工程copy到本地并建议重命名,这样方便以后管理例如:



第三步:设置Library

1..将两个工程设置为Library

2..在主工程中引入这两个Library例如:



通过这三步就可以将这两个包导入进来了。



Android Studio


Android Stuido相对于Eclipse简单的多:

第一步:

首先要确保已经将Android Support Libraries升级到最新.


第二步:

打开项目中的build.gradle文件,在dependencies中添加如下代码。

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. dependencies {  
  2.     compile 'com.android.support:recyclerview-v7:21.+'  
  3.     compile 'com.android.support:cardview-v7:21.+'  
  4. }  


第三步:

重新Build一下工程。


Build完成后就会发现这两个包就已经导入进来了





代码介绍:


主题:


首先这个黑色基调的主题是使用了Material.Dark.ActionBar样式。


设置方法:修改values-v21文件夹下styles.xml文件:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <resources>  
  2.     <style name="AppTheme" parent="android:ThemeOverlay.Material.Dark.ActionBar">  
  3.     </style>  
  4. </resources>  


布局文件:

recycler_view.xml(RecyclerView布局文件):

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context=".MyActivity">  
  7.   
  8.     <android.support.v7.widget.RecyclerView  
  9.         android:id="@+id/list"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         tools:context=".MyActivity" />  
  13. </FrameLayout>  
FrameLayout里包含了RecyclerView控件



card_view.xml(CardView布局文件):

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:card_view="http://schemas.android.com/apk/res-auto"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:layout_margin="5dp"  
  6.     android:orientation="horizontal"  
  7.     card_view:cardBackgroundColor="@color/cardview_dark_background"  
  8.     card_view:cardCornerRadius="5dp" >  
  9.   
  10.     <RelativeLayout  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="100dp"  
  13.         android:padding="5dp" >  
  14.   
  15.         <ImageView  
  16.             android:id="@+id/pic"  
  17.             android:layout_width="match_parent"  
  18.             android:layout_height="match_parent"  
  19.             android:layout_centerInParent="true"  
  20.             android:scaleType="centerCrop" />  
  21.   
  22.         <TextView  
  23.             android:clickable="true"  
  24.             android:id="@+id/name"  
  25.             android:layout_width="match_parent"  
  26.             android:layout_height="match_parent"  
  27.             android:layout_marginBottom="10dp"  
  28.             android:layout_marginRight="10dp"  
  29.             android:gravity="right|bottom"  
  30.             android:textColor="@android:color/white"  
  31.             android:textSize="24sp" />  
  32.     </RelativeLayout>  <li class="alt" style="margin: 0px !imp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值