Layout

MffiAIHM LinearLayout fP RelativeLayout MfifiMmA®AA7 - AftA

Step-1: MA Android JffifflgA*^ : ex08 = Step-2: MM Activity lAA^ ex08 > ggAMT ■

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);

RelativeLayout r_layout = new RelativeLayout(this); setContentView(r_lay out);

TableLayout tableLayout = new TableLayout(this);

r_layout.addView(tableLayout, new RelativeLayout.LayoutParams(WC, WC)); tableLayout. setId( 1);

TableRow tableRowi = new TableRow(this);

tableLayout. addView(tableRow1, new TableLayout.LayoutParams(WC, WC));

Image View iv = new ImageView(this); tableRow1.addView(iv);

iv.setImageDrawable(getResources().getDrawable(R.drawable.5/ar_6ig_on));

EditText edit1 = new EditText(this);

tableRow1.addView(edit1);

TableRow tableRow2 = new TableRow(this);

Image View iv2 = new ImageView(this);

iv2.setImageDrawable(getResources().getDrawable(R.drawable.ga//er>_pAofo_4)); tableRow2. addView(iv2); EditText edit2 = new EditText(this); tableRow2.addView(edit2);

tableLayout.addView(tableRow2, new TableLayout.LayoutParams(WC, WC));

Button btn = new Button(this); btn.setText("Exit"); btn. setOnClickListener(this); RelativeLayout.LayoutParams param =

new RelativeLayout.LayoutParams(WC, WC); param. addRule(RelativeLay out. BELOW, 1); param.topMargin = 20; r_layout.addView(btn, param);

public void onClick(View arg0) { finish(); }

Linda

0936-345678

Wander

0931-543211

Tom

0945-678888

RelativeLayout TableLayout

TableRow EditText

Button

ImageView

RelativeLayout TableLayout

TableRow EditText

Button

ImageView

3. J^ftAAÄMAftview

RelativeLayout r_layout = new RelativeLayout(this); setContentView(r_layout)

4. TableLayout ' MJPA^ r_layout U TableLayout tableLayout = new TableLayout(this);

r_layout.addView(tableLayout, new RelativeLayout.LayoutParams(WC, WC));

5. SA^SAMft TableRow > MJPA^ tableLayout U :

TableRow tableRow1 = new TableRow(this);

tableLayout. addView(tableRow1, new TableLayout.LayoutParams(WC, WC));

ImageView iv = new ImageView(this); tableRow 1. addView(iv);

□ Slffla 10:38 AM 1

ex09

Item-1

Item-2

Item-3

Step-1: Android MMA*^ : ex09 = Step-2: MM Activity iftAS^ ex09 >

package com.misoo.ex09; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.Layoutlnflater; import android.view.Menultem; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView;

public class ex09 extends Activity {

public static final int CHGID = Menu.FIRST; public static final int EXITID = Menu.FIRST + 1; private String[] data = {" Item-1", " Item-2", " Item-3"}; TextView tv; LinearLayout layout1, layout2; boolean selection = true;

@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); Layoutlnflater inflate = (Layoutlnflater)

getSy stemService(Context.LAYOUT_INFLATER_SERVICE); layout1 = (LinearLayout)inflate.inflate(R.layoutmain, null); setContentView(layout 1);

TextView tx = (TextView)findViewById(Rid.ix); tx.setBackgroundResource(R.drawable.£g_6); tx. setTextColor(Color. WHITE);

layout2 = (LinearLayout)inflate.inflate(R.layout./i5/, null); ListView lv = (ListView)layout2.findViewById(R.idiist); lv.setBackgroundResource(R.drawable.desc_new); Array Adapter<String> adapter = new ArrayAdapter<String>

(this, android.R.layout.simple_list_item_1, data); lv.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, CHGID, 0, "Change");

Menultem im = menu.findItem( CHGID); im.setIcon(R.drawable. changeim); menu.add(0, EXITID, 1, "Exit");

im = menu.findItem(EXT_ID); im.setIcon(R.drawable. exitmenuitem); return true; }

@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case CHGID: selection = ! selection; if(selection) setContentView(lay out 1); else setContentView(layout2); break;

return super.onOptionsItemSelected(item);

Step-3: MM/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" > <ListView android:id="@+id/list"

android:layout_width="wrap_content" android:layout_height="wrap_content" />

</LinearLayout>

0 0

Post a comment