adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
//------------------------------------------------------
Button btn1 = new Button(this);
btn1.setId(2);
btn1.setText("OK");
btn1.setOnClickListener(listener);
param = new RelativeLayout.LayoutParams(60, WC);
param.addRule(RelativeLayout.RIGHT_OF, 1);
param.leftMargin = 15;
param.topMargin = 20;
r_layout.addView(btn1, param);
//------------------------------------------------------
Button btn2 = new Button(this);
btn2.setId(3);
btn2.setText("Exit");
btn2.setOnClickListener(listener);
param = new RelativeLayout.LayoutParams(60, WC);
param.addRule(RelativeLayout.BELOW, 2);
param.addRule(RelativeLayout.ALIGN_LEFT, 2);
param.topMargin = 25;
r_layout.addView(btn2, param);
}
OnClickListener listener = new OnClickListener()
{
public void onClick(View v) {
if(v.getId() == 2)
tv.setText(selection);
else if(v.getId() == 3)
finish();
}
};
public void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
setTitle(data[arg2]);
selection = data[arg2];
}
}
package com.misoo.ex08;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
public class ex08 extends Activity implements OnClickListener {
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
@Override public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//根据下面所说的嵌套层次,首先是定义最大的view,并设定为目前布局(this值)
RelativeLayout r_layout = new RelativeLayout(this);
setContentView(r_layout);
//当书面上的一群view排成一个矩阵或者是表格形式时,就采用TableLayout来叙述其布局方式。
//接着定义第二个的TableLayout,并加入到r_layout里
TableLayout tableLayout = new TableLayout(this);
r_layout.addView(tableLayout, new RelativeLayout.LayoutParams(WC, WC));
tableLayout.setId(1);
//此程序采用的嵌套层次为:RelativeLayout->TableLayout->TableRow->EditText->Button->ImageView
//再定义第三个的TableRow,并加入到tableLayout里
TableRow tableRow1 = new TableRow(this);
tableLayout.addView(tableRow1, new TableLayout.LayoutParams(WC, WC));
//最后定义最小的view,并加入到tableRow里
ImageView iv = new ImageView(this);
tableRow1.addView(iv);
iv.setImageDrawable(getResources().getDrawable(R.drawable.star_big_on));
EditText edit1 = new EditText(this);
tableRow1.addView(edit1);
//-------------------------------------------------
TableRow tableRow2 = new TableRow(this);
tableLayout.addView(tableRow2, new TableLayout.LayoutParams(WC, WC));
ImageView iv2 = new ImageView(this);
iv2.setImageDrawable(getResources().getDrawable(R.drawable.gallery_photo_4));
tableRow2.addView(iv2);
EditText edit2 = new EditText(this);
tableRow2.addView(edit2);
//-------------------------------------------------
Button btn = new Button(this);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(WC, WC);
param.addRule(RelativeLayout.BELOW, 1);
param.topMargin = 20;
r_layout.addView(btn, param);
btn.setText("Exit");
btn.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
}