一、常用类:
1. Activity 是最基本的类,它代表一个显示页面。类似一个 Servlet,可以显示页面、捕捉事件、显示菜单、处理复杂的用户交互等。
2. Intent 指一个目标。它包含 Action、Name。Action指定目标动作, Name指定目标类地址。
Intent典型用法如下:
Intent intent=new Intent(thisContext,toClass);
intent.putExtra(Bundle xx);//为Intent添加数据
startActivity(intent);//发送该intent
//另外还可以配置Intent Return来处理Intent数据返回。
//调用startActivityForResult(intent)即可实现该层嵌套。
3.对于典型的一些数据控件,比如ListView、TabHost。Android对其都进行了简单封装和布局定义,对应有ListActivity、TabActiviy等。
4. Android采用了典型的MVC结构。其表现如下:View (界面)既可以通过xml(layout目录下)生成,也可以通过硬编码(代码)的方式直接通过代码生成。对于xml中的View资源,可以在代码中通过getViewById()的方法获得。Model既可以通过xml(values目录下)生成,也可以硬编码的方式直接在代码中指定。View和Model通过Adapter来进行连接。典型的Adapter包括ArrayAdapter(可以Sort()操作)、CusorAdapter(从Cusor中查询到数据源),ListAdapter、SimpleAdapter(最常用)、SpinnerAdapter(它是一个接口,设置Spinner应用SimpleAdapter的setDropDownResource方法)。
5. SimpleAdapter典型用法:
List<Map<String,String>> list=new ArrayList();
Map<String,String> map=new HashMap();//代表列表中的一个项。Key值将决定Value的显示位置。
map.put(“name”, “WangFeng”);
map.put(“description”, “I am a Student.”);
list.add(map);
new SimpleAdapter(
this, //Context
listdata,//List<Map<String,?>>,List<Map<?,String>> or Cursor
android.R.layout.simple_list_item_2,//which view display
new String[]{“name”,”description”},//data column Name
new int[]{android.R.id.text1,android.R.id.text2};//which data view display
ArrayAdapter的典型用法:
注意ArrayAdapter是一个泛型对象,其泛型类型与数据源的array class对应。ArrayAdapter仅有一列数据。因此内置了insert(),remove(),add(),clear()等数据操作方法。同时还提供了sort(Compartor)的排序方法。
new ArrayAdapter(
this,//Context
android.R.layout. simple_list_item_1,//which view
array//array object or resource id
)
二、开发陷阱:
1.在调用super.onCreate()方法之前,Activity的Context尚处于null状态。切勿在onCreate方法前初始化View组件。
2.在调用this.setContentView(xx.xml)之前,切勿对该layout文件使用findViewById()方法,否则将得到null结果。
3.使用ListView时,必须在setAdapter()方法之前调用setHeader()、setFooter()、setEmptyView(),否则将抛出异常。另外,不要尝试添加一个复杂的view放在listView的header里面,这会影响事件的捕获。
4.对于一个继承AdapterView的对象,切勿去捕捉Clicked、Selected事件,而应改用onItemClicked(),onItemSelected()事件。否则将抛出异常。
5.如ListView的数据源发生了改变。应调用notifyDataSetChanged()方法来更新视图。不过SimpleAdapter不提供notifyDataSetChanged()方法。更新以SimpleAdapter为桥梁的视图只能采用重新setAdapter()的方法。
6.对于布局文件,在嵌套LinearLayout时,请尽量使用wrap_content。使用match_parent将可能覆盖父容器,并导致无法显示后面的布局。
7.Android中的Calendar默认是处于GMT+0:0的时区。因此其Date对象与本机Date差值8小时。
三、Android开源工具、项目:
1.android/tools目录下有个叫Hierarchy Viewer的工具,在启动模拟器后运行该bat文件将加载应用界面。可以帮助界面调优。
2.DroidDraw:是一个开源的Java桌面工具。运行后可以对Android界面进行可视化编辑。不过DroidDraw与Eclipse的编码好像有问题。DroidDraw开发AbsoluteLayout较有优势。普通编辑可视化layout可以使用ADT内置的工具,使用方法是进入xml文件,选择layout标签。
3.ChartEngin:Android平台下的一个免费项目,用于显示各种报表。
4.HessianDroid:Hessian的android版本,使用Hessian可以完成轻量级RPC对象传输。
package com.dev.multicolumn.listview;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class MultiColumnListView extends Activity {
/** Called when the activity is first created. */
// I use HashMap arraList which takes objects
private ArrayList <HashMap<String, Object>> myBooks;
private static final String BOOKKEY = "bookname";
private static final String PRICEKEY = "bookprice";
private static final String IMGKEY = "iconfromraw";
private static final String RATINGKEY = "ratings";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.list);
myBooks = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> hm;
//With the help of HashMap add Key, Values of Book, like name,price and icon path
hm = new HashMap<String, Object>();
hm.put(BOOKKEY, "Android");
hm.put(PRICEKEY, "Price Rs: 500");
hm.put(IMGKEY, R.raw.android); //i have images in res/raw folder
hm.put(RATINGKEY, 2);
myBooks.add(hm);
hm = new HashMap<String, Object>();
hm.put(BOOKKEY, "PHP");
hm.put(PRICEKEY, "Price Rs: 250");
hm.put(IMGKEY, R.raw.php);
hm.put(RATINGKEY, 1);
myBooks.add(hm);
hm = new HashMap<String, Object>();
hm.put(BOOKKEY, "Java");
hm.put(PRICEKEY, "Price Rs: 399");
hm.put(IMGKEY, R.raw.java);
hm.put(RATINGKEY,3);
myBooks.add(hm);
hm = new HashMap<String, Object>();
hm.put(BOOKKEY, "C++");
hm.put(PRICEKEY, "Price Rs: 450");
hm.put(IMGKEY, R.raw.cplusplus);
hm.put(RATINGKEY, 2);
myBooks.add(hm);
//
// SimpleAdapter adapter = new SimpleAdapter(this, myBooks, R.layout.listbox,
//new String[]{BOOKKEY,PRICEKEY,IMGKEY}, new int[]{R.id.text1, R.id.text2, R.id.img});
// ListAdapter adapter = new myListAdapter(myBooks);
listView.setAdapter(new myListAdapter(myBooks,this));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
private class myListAdapter extends BaseAdapter{
private ArrayList<HashMap<String, Object>> Books;
private LayoutInflater mInflater;
public myListAdapter(ArrayList<HashMap<String, Object>> books, Context context){
Books = books;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Books.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return Books.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listbox, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.v = (TextView) convertView.findViewById(R.id.text1);
holder.v1 = (TextView) convertView.findViewById(R.id.text2);
holder.icon = (ImageView) convertView.findViewById(R.id.img);
holder.rating = (RatingBar)convertView.findViewById(R.id.star);
convertView.setTag(holder);
}else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data with the holder.
holder.v.setText((String) Books.get(position).get(BOOKKEY));
holder.v1.setText((String) Books.get(position).get(PRICEKEY));
holder.icon.setImageResource((Integer)Books.get(position).get(IMGKEY));
holder.rating.setRating((Integer)Books.get(position).get(RATINGKEY));
return convertView;
}
static class ViewHolder {
TextView v;
TextView v1;
ImageView icon;
RatingBar rating;
}
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:orientation="horizontal" android:layout_height="wrap_content"> <LinearLayout android:layout_width="265dip" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/text1" android:textSize="25dip" android:text="This is text1"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/text2" android:text="This is text2"/> <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/star" android:numStars="10" android:stepSize="0.1" android:isIndicator="true" /> </LinearLayout> <ImageView android:layout_width="55dip" android:layout_height="fill_parent" android:id="@+id/img" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/list" /> </LinearLayout>
在之前的版本中,只有一个drawable,而2.1版本中有drawable-mdpi、drawable-ldpi、drawable-hdpi三个,这三个主要是为了支持多分辨率。
drawable- hdpi、drawable- mdpi、drawable-ldpi的区别:
(1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854)
(2)drawable-mdpi里面存放中等分辨率的图片,如HVGA (320x480)
(3)drawable-ldpi里面存放低分辨率的图片,如QVGA (240x320)
系统会根据机器的分辨率来分别到这几个文件夹里面去找对应的图片,在开发程序时为了兼容不同平台不同屏幕,建议各自文件夹根据需求均存放不同版本图片。