Android add TextView components and ImageView components dynamically

Source: Internet
Author: User
Tags gettext

Import android.app.Activity;

Import Android.os.Bundle;

Import android.view.Gravity;

Import Android.view.View;

Import Android.view.ViewGroup;

Import Android.widget.AdapterView;

Import Android.widget.BaseAdapter;

Import Android.widget.Gallery;

Import Android.widget.ImageView;

Import Android.widget.LinearLayout;

Import Android.widget.ListView;

Import Android.widget.TextView;

Import Android.widget.AdapterView.OnItemClickListener;

Import Android.widget.AdapterView.OnItemSelectedListener;

public class Sample_5_4 extends Activity {

all resource images (Andy,Bill,Edgar,torvalds, Turing) An array of IDs

Int[] Drawableids=

{r.drawable.andy,r.drawable.bill,r.drawable.edgar,r.drawable.torvalds,r.drawable.turing};

All resource strings (Andy,Bill,Edgar,torvalds ,Turing) An array of IDs

Int[] msgids={r.string.andy,r.string.bill,r.string.edgar,r.string.torvalds,r.string.turing};

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

ListView lv= (ListView) This.findviewbyid (R.ID.LISTVIEW01);// Initialize listview

Baseadapter ba=new Baseadapter () {// Prepare content adapter for ListView

public int GetCount () {return 5;} A total of 5 Options

Public Object getItem (int arg0) {return null;}

public long getitemid (int arg0) {return 0;}

Public View getView (int arg0, view arg1, ViewGroup arg2) {

dynamically generate the viewfor each drop-down item, each drop-down view by linearlayout

contains a ImageView and a TextView composition

LinearLayout ll=new LinearLayout (sample_5_4.this);// Initialize linearlayout

Ll.setorientation (linearlayout.horizontal); Set Orientation

Ll.setpadding (5,5,5,5);// set aside white around

ImageView ii=new ImageView (sample_5_4.this);// Initialize ImageView

Ii.setimagedrawable (Getresources (). getdrawable (drawableids[arg0]));// Set Picture

Ii.setscaletype (ImageView.ScaleType.FIT_XY);

Ii.setlayoutparams (New Gallery.layoutparams (100,98));

Ll.addview (ii);// Add to linearlayout

TextView tv=new TextView (sample_5_4.this);// Initialize TextView

Tv.settext (Getresources (). GetText (msgids[arg0]));// Set content

Tv.settextsize ()// set Font size

Tv.settextcolor (Sample_5_4.this.getResources (). GetColor (R.color.white));// Set Font color

Tv.setpadding (5,5,5,5);// set aside white around

Tv.setgravity (Gravity.left);

Ll.addview (TV);// Add to linearlayout

return ll;

}

};

Lv.setadapter (BA);// Set content adapter for ListView

Lv.setonitemselectedlistener (// SET option selected Listener

New Onitemselectedlistener () {

public void onitemselected (adapterview<?> arg0, View arg1,

int arg2, long arg3) {// override option handled by selected event

TextView tv= (TextView) Findviewbyid (R.ID.TEXTVIEW01);// Get the main interface TextView

LinearLayout ll= (linearlayout) arg1;// Gets the linearlayout of the currently selected option

TextView tvn= (TextView) Ll.getchildat (1);// get one of the TextView

StringBuilder sb=new StringBuilder ();// Generate information dynamically with StringBuilder

Sb.append (Getresources (). GetText (R.string.ys));

Sb.append (":");

Sb.append (Tvn.gettext ());

String stemp=sb.tostring ();

Tv.settext (Stemp.split ("\\n") [0]);// information set into the main interface TextView

}

public void onnothingselected (Adapterview<?> arg0) {}

}

);

Lv.setonitemclicklistener (// set option clicked Listener

New Onitemclicklistener () {

public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,

Long Arg3) {// override options How to handle a clicked event

TextView tv= (TextView) Findviewbyid (R.ID.TEXTVIEW01);// Get the main interface TextView

LinearLayout ll= (linearlayout) arg1;// Gets the linearlayout of the currently selected option

TextView tvn= (TextView) Ll.getchildat (1);// get one of the TextView

StringBuilder sb=new StringBuilder ();// Generate information dynamically with StringBuilder

Sb.append (Getresources (). GetText (R.string.ys));

Sb.append (":");

Sb.append (Tvn.gettext ());

String stemp=sb.tostring ();

Tv.settext (Stemp.split ("\\n") [0]);// information set into the main interface TextView

}

}

);

}

}

<?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/textview01"

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

Android:textsize= "24dip"

Android:textcolor= "@color/white"

android:text= "@string/hello"/>

<listview

Android:id= "@+id/listview01"

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

Android:choicemode= "Singlechoice"/>

</LinearLayout>

Android add TextView components and ImageView components dynamically

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.