The. XML code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" ><gallery android:id= "@+id/gallery" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:gravity= "center_vertical" android:spacing= "3px" /> </LinearLayout>
The interface Imagegalleryadapter.java code is as follows:
Package Org.lxh.demo;import Android.content.context;import Android.view.view;import android.view.viewgroup;import Android.view.viewgroup.layoutparams;import Android.widget.baseadapter;import Android.widget.Gallery;import Android.widget.imageview;public class Imagegalleryadapter extends Baseadapter {private Context context;private int imgres[] = new int[] {r.drawable.ispic_a, r.drawable.ispic_b,r.drawable.ispic_c, r.drawable.ispic_d};p ublic Imagegalleryadapter (Context context) {This.context = context;} public int GetCount () {return this.imgRes.length;} Public Object getItem (int arg0) {return this.imgres[arg0];} public long getitemid (int positon) {return This.imgres[positon];} Public View getView (int positon, view arg1, ViewGroup arg2) {ImageView img = new ImageView (this.context); Img.setimageresou Rce (This.imgres[positon]); Img.setscaletype (ImageView.ScaleType.CENTER); Img.setlayoutparams (new Gallery.layoutparams (layoutparams.wrap_content,layoutparams.wrap_content)); return img;}}
Main program. Java code is as follows:
Package Org.lxh.demo;import Android.app.activity;import Android.app.alertdialog;import android.app.Dialog;import Android.app.progressdialog;import Android.content.dialoginterface;import Android.os.bundle;import Android.text.method.scrollingmovementmethod;import Android.view.layoutinflater;import Android.view.View;import Android.view.view.onclicklistener;import Android.view.viewgroup.layoutparams;import Android.view.animation.animationutils;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.button;import Android.widget.gallery;import Android.widget.imageswitcher;import Android.widget.imageview;import Android.widget.seekbar;import Android.widget.SeekBar.OnSeekBarChangeListener; Import Android.widget.spinner;import Android.widget.textview;import Android.widget.toast;import Android.widget.viewswitcher.viewfactory;public class Hello extends Activity {private Gallery Gallery=null; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Life cycle Method Super.setcontentview (R.layout.main); Set the layout manager this.gallery= (gallery) Super.findviewbyid (r.id.gallery) to be used, this.gallery.setAdapter (new Imagegalleryadapter (this));}}
The results are as follows:
Android--Gallery (Gallery) components