在去年Google发布了代号AndroidL(版本号Android5.0,SDK为API 21)后,Android的新特性就为开发者所津津乐道,尤其是CardView和RecyclerView。使界面设计更加美观,更加方便。本篇文章来聊聊如何在Eclipse中使用AndroidL新特性CardView来进行开发。由于大家手上一般都没有Android 5.0设备,所以最好装好Genymotion模拟器来进行运行。

具体实现步骤如下:

(1)新建Android项目,要求TargetSDK和Compile With都至少选择API 21 :Android 5.0及以上。


(2)打开Eclipse中的SDK Manager,要求装好Android 5.0,API 21下面的安装包。(该过程可能会很慢,耐心等。)


(3)同样在这里,在Extras目录下,要求至少装好Android Support Library,且版本至少为21.即对应了API 21.因为CardView是在support.v7包下面的。所以要更新这一步。(也可能会比较慢,你懂的)


(4)然后在Eclipse中导入CardView依赖库。import项目:


(5)然后找到cardview所在位置。目录为:Android SDK源目录-->extras-->android-->suuport-->v7-->cardview.然后选中Copy project into workspace.即复制到工作空间。


(6)此时为了cardview项目和原先的项目有所区分,修改项目名称为cardview_lib_v7.即该项目是不能直接运行的,是作为其他项目的依赖库存在的。

(7)然后选中cardview_lib_v7,右键-->properties-->Android.在上方选中Android 5.0,下方选中IsLibrary。Apply-->OK.这样该项目就能作为别的项目的库了。


(8)在我们的项目CardViewDemo中引入刚才的cardview_lib_v7.CardViewDemo右键-->Properties-->Android-->Add. 选中cardview_lib_v7,OK。然后Apply-->OK.


.


.


(9)这样就能成功导入了cardview_lib_v7依赖包,查看CardViewDemo项目中的Android Dependencies,可以发现已经成功引入了jar包。


(10)现在就可以使用CardView进行开发了。本例子只是用CardView做了简单的例子,复杂的我们以后再分享。具体代码如下:

xml:

<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:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_margin="5dp"    android:orientation="vertical"    card_view:cardBackgroundColor="@color/cardview_light_background"    card_view:cardCornerRadius="5dp" >    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:padding="5dp" >        <TextView            android:id="@+id/title"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="top|center_horizontal"            android:text="标题:第一个CardView程序"            android:textColor="@android:color/black"            android:textSize="25sp" />        <ImageView            android:id="@+id/pic"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@id/title"            android:layout_centerInParent="true"            android:scaleType="centerCrop"            android:src="@drawable/pic" />        <TextView            android:id="@+id/desc"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignBottom="@+id/pic"            android:layout_alignRight="@+id/pic"            android:layout_marginRight="10dp"            android:text="风景很美"            android:textColor="@android:color/white"            android:textSize="24sp" />        <TextView            android:id="@+id/text"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@id/pic"            android:text="正文:Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。Android操作系统最初由Andy Rubin开发,主要支持手机。2005年8月由Google收购注资。2007年11月,Google与84家硬件制造商、软件开发商及电信营运商组建开放手机联盟共同研发改良Android系统。"            android:textColor="@android:color/black"            android:textSize="25sp" />        <EditText            android:id="@+id/input"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@id/text"            android:hint="请输入内容" />        <TextView            android:id="@+id/date"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@id/input"            android:text="这里会显示日期"            android:textSize="30sp" />    </RelativeLayout></android.support.v7.widget.CardView>


java:

package com.example.cardviewdemo;import java.text.SimpleDateFormat;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.widget.TextView;public class MainActivity extends Activity {private TextView dateText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);dateText = (TextView) findViewById(R.id.date);SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd    hh:mm:ss");String date = sDateFormat.format(new java.util.Date());dateText.setText(date);}}

(11)点击运行即可。可能在第一次运行的时候会出现以下奇怪的错误,只要重新Clean一下工作空间即可,或者刷新一下项目。



(12)最后显示的效果如下:


总结一下,可能在这个例子中还没有看出CardView的优秀之处,因为先了解下如何使用CardView,具体学习复杂设计我们以后再分享。CardView是继承自FreamLayout的,也可以理解为一种布局,或者是容器,可以存放其他的控件。这个容器可以设置很多的属性,如背景颜色,圆角等等。下篇帖子我们研究下如何在AndroidStudio中使用CardView。


github主页:https://github.com/chenyufeng1991 。欢迎大家访问!

更多相关文章

  1. 使用Android注解处理器,解放劳动力
  2. 推荐几个非常有用的开发工具之Android(安卓)Studio插件
  3. Android使用开源框架ANDROID-IMAGE-INDICATOR实现图片轮播部署
  4. Android伸手党系列之四:Android项目开发常用技术
  5. Android水波纹点击效果
  6. Android(安卓)studio高效开发之——快捷键的使用
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. 阿拉伯语布局修改总结
  2. android退出系统的绝杀方法
  3. Android, repo sync经常失败的解决办法
  4. Android系统基础(02) 系统源码环境搭建
  5. android学习笔记——Intent和IntentFilte
  6. Android中五大字符串总结(String、StringB
  7. 了解adb
  8. Android主线程、子线程通信(Thread+handle
  9. 19、Android之AutoComplete
  10. Android三种方式实现ProgressBar自定义圆