Eclipse中使用Android5.0新特性CardView

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

具体实现步骤如下:

(1)新建Android项目,要求Target SDK和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,下方选中Is Library。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;

	@Override
	protected 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的,也可以理解为一种布局,或者是容器,可以存放其他的控件。这个容器可以设置很多的属性,如背景颜色,圆角等等。下篇帖子我们研究下如何在Android Studio中使用CardView。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值