PMMfflOTffiMRelative Layout

rnmmfrmm. LinearLayout M ' XffiM^XIH^-fi^fflftM °

2. Sft^l 4 MSXft view ' 1 M TextView * —f® ListView fPMMfôS

A/res/drawable/bk_red.jpg MM ListView ^ffBt * If bk_ok_exit^jpg mmmmimmmmmm ° mr ■

+ B Android Library

3 & drawable

3SHHJHI

[£1 icon.png $ & layout _ list.xrnl I ■ ; :- |i| rnam.xml

Step-2: MM Activity lAA^ ex07 > ^MAMtA : // ---- ex07.java MAM

package com.misoo.ex07; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.Layoutlnflater; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView;

import android.widget.AdapterView.OnItemClickListener;

public class ex07 extends Activity implements OnltemClickListener {

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_lay out); LayoutInflater inflate = (Layoutlnflater)

getSystemService(ContextLLAYOUT_INFLATER_SERVICE); LinearLayout layout = (LinearLayout)inflate.inflate(R.layoutlist, null); RelativeLayoutLayoutParams param

= new RelativeLayout.LayoutParams(175, WC); layout setId(1);

r_layout.addView(layout, param); tv = (TextView)layout.findViewById(R.id.text); ListView lv = (ListView)layoutfindViewById(R.id.list); lv.setBackgroundResource(R.drawable.M_red); adapter = new ArrayAdapter<String>

(this, android.R.layout.simple list item multiple choice, data); lv.setAdapter(adapter); lv.setltemsCanFocus(false); lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lv.setOnItemClickListener( this);

Button btnl = new Button(this);

btnLsetBackgroundResource(R.drawable.M_o/_exit);

btn1.setId(2); btn1.setText("OK"); btnl.setOnClickListener(listener);

param = new RelativeLayout.LayoutParams(60, WC);

param. addRule(RelativeLay out. RIGHTOF, 1);

param. leftMargin = 15; param. topMargin = 20;

r_layout.addView(btn1, param);

Button btn2 = new Button(this);

btn2.setBackgroundResource(R.drawable.M_o/_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);

OnClickListener listener = new OnClickListener() {

public void onClick(View v) { if(v.getId() == 2){ String ss = "{";

if(status[i]) { ss += data[i]; ss += " "; }

public void onItemClick(AdapterView<?> arg0, View v, int idx, long arg3) { status [idx] = ! status [idx]; }

<?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" > <Text View 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>

»TOftfflfi^M^ ° mWffi^ ' ft^fflM view_y ftfuTO ' —f® view_x ' Mmmm view_y fuj^ view_x ftT^ ' ffl^ 15 '

2. W^rnSHA • RelativeLayout r_layout = new RelativeLayout(this);

setContentView(r_layout);

3. HA- Layoutlnflater inflate = (Layoutlnflater) getSystemService(

Context.LAYOUTINFLATERSERVICE);

LAYOUT_INFLATE_SERVICE Sg^ftiff ' ^AM inflate =

LinearLayout layout = (LinearLayout)inflate.inflate(R.layout.list, null, null);

^^inflateiffftinflate()®^^a/res/layout/list.xmlAA®M^A—ffl LinearLayoutiff°

5. HA- RelativeLayout.LayoutParams param

= new RelativeLayout.LayoutParams(120, WC);

MA^i® RelativeLayout layout MaffiM

r_layout.addView(layout, param);

ll layout —i ID f® ' ^^ffi layout JpA^ r_layout ffi °

layout

7. HA : tv = (TextView)layout.findViewByld(Rid.text);

ListView lv = (ListView)layout.findViewByld(R.id.list);

8. ArrayAdapter<String> adapter = new ArrayAdapter<String>

(this, android.R.layout.simple list item l, data); lv.setAdapter(adapter);

9. ggA : tv = (TextView)layout.findViewByld(R.id.text);

ListView lv = (ListView)layout.findViewById(R.id.list);

Am^AlayoutgftMfflAView

btn1.setId(2);

btn1.setText("OK");

btn1.setOnClickListener(listener);

11. fgA • param = new RelativeLayout.LayoutParams(60, WC);

param.addRule(RelativeLayout.R/G#7_OF, 1); param.leftMargin = 15; param.topMargin = 20; r_layout.addView(btn1, param);

• SM60i£. f^layoutftA» • «mm • MA» AA»#20IA®^#btn1JnA^r_layoutffi°

0 0

Post a comment