在去年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。

更多相关文章

  1. Android(安卓)NoSQL之SnappyDB
  2. Android商城购物车页面实现和逻辑实现
  3. [Android] 基于 Linux 命令行构建 Android(安卓)应用(五):Ant 构建
  4. Android(安卓)NDK学习之 一. Android(安卓)NDK简介
  5. apk打包
  6. 使用android快速开发框架afinal的FinalDb操作android sqlite数据
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. Android漏洞
  2. Android调用系统camera
  3. Android(安卓)Studio Unknown host ‘dow
  4. 横、竖分割线
  5. 监测Android(安卓)2G CMCC Signal Streng
  6. ADROID2.2系统多国语言定制
  7. Android(安卓)Studio2.1.3之后在小米手机
  8. Android: ViewDragHelper tutorial
  9. android notification 示例
  10. 查找所有activity