public class ex07 extends Activity implements OnItemClickListener { private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; private String[] data = {" Item-1 ", " Item-2 ", " Item-3 "}; private TextView tv; private String selection = "****"; private ArrayAdapter<String> adapter; private boolean[] status = {false, false, false }; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); RelativeLayout r_layout = new RelativeLayout(this); setContentView(r_layout); LayoutInflater inflate = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); //定义一个过滤器,用来处理布局的筛选 LinearLayout layout = (LinearLayout)inflate.inflate(R.layout.list, null); RelativeLayout.LayoutParams param= new RelativeLayout.LayoutParams(175, WC); layout.setId(1); //定义了一个list布局,用来存放listview的布局 r_layout.addView(layout, param); //将list布局加入到relative类型布局中 tv = (TextView)layout.findViewById(R.id.text);//一个TextView ListView lv = (ListView)layout.findViewById(R.id.list);//一个listview lv.setBackgroundResource(R.drawable.bk_red); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, data); lv.setAdapter(adapter); lv.setItemsCanFocus(false);//将Adapter加入到listview中 lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//多选模式 lv.setOnItemClickListener( this); Button btn1 = new Button(this); btn1.setBackgroundResource(R.drawable.bk_ok_exit); 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);//将param用到了view之间的布局中 //将btn1这个view加入到layout中
//透過參數物件來敘述,按鈕寬60點,位於layout的右邊,相距15點,而且距離上方邊界20點。最後將btn1加入到r_layout裡
Button btn2 = new Button(this); btn2.setBackgroundResource(R.drawable.bk_ok_exit); 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); //将btn2加入到layout中 } OnClickListener listener = new OnClickListener() {public void onClick(View v) { if(v.getId() == 2){ String ss = "{";for(int i=0; i< adapter.getCount(); i++){if(status[i]) { ss += data[i]; ss += " "; }} ss += "}";setTitle(ss);} else if(v.getId() == 3) finish();}}; //针对不同的选项做出的判断 public void onItemClick(AdapterView<?> arg0, View v, int idx, long arg3) { status[idx] = ! status[idx]; }}
很明显的看到在Relative的layout中的各个view在布局中的位置是通过parm来自行确定位置的
补充说明中
RelativeLayout r_layout = new RelativeLayout(this); setContentView(r_layout); // ……… LayoutInflater inflate = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE); LinearLayout layout = (LinearLayout)inflate.inflate(R.layout.list, null, null); // ………. tv = (TextView)layout.findViewById(R.id.text);
这样的一个过程,採用的佈局是r_layout,而不是layout,所以不能直接寫為:
tv = (TextView)findViewById(R.id.text);
反之,如果是:
RelativeLayout r_layout = new RelativeLayout(this); setContentView(R.layout.list); tv = (TextView)findViewById(R.id.text);
就對了。因為list.xml 是目前生效的佈局。
最后看看/res/layout/list.xml 的內容如下:
<?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" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World, ex07" /> <ListView android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>