完全用代码实现ListView,不涉及任何xml的layout
package com.pekall;import java.util.ArrayList;import android.app.Activity;import android.app.ListActivity;import android.content.ContentUris;import android.content.Context;import android.content.Intent;import android.database.Cursor;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.Bitmap.Config;import android.graphics.Paint.Style;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.view.ContextMenu;import android.view.KeyEvent;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.view.ContextMenu.ContextMenuInfo;import android.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ListView listview = new ListView(this);MyAdapter adapter = new MyAdapter(this, aa);listview.setAdapter(adapter);LinearLayout main = new LinearLayout(this);main.addView(listview);setContentView(main);}String[] aa = {"11111111111","222222222222","3333333333"};}class MyListItem extends LinearLayout {TextView tv;public MyListItem(Context context, String text) {super(context);this.setOrientation(HORIZONTAL);tv = new TextView(context);tv.setText(text);addView(tv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));}}class MyAdapter extends BaseAdapter {Context ctx;String[] array;public MyAdapter(Context ctx, String[] array) {this.ctx = ctx;this.array = array;}public int getCount() {return array.length;}public Object getItem(int position) {return null;}public long getItemId(int position) {return position;}public View getView(int position, View convertView, ViewGroup parent) {LinearLayout l = new MyListItem(ctx,array[position]);return l;}}} 这样可以定制一个item,主要用于学习之用,还是建议能在xml中实现的尽量别用代码,因为维护xml文件相对容易很多