Package xiaosi. gallery; import android. app. activity; import android. content. context; import android. content. res. typedarray; import android. OS. bundle; import android. view. view; import android. view. viewgroup; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistener; import android. widget. baseadapter; import android. widget. gallery; import android. widget. imageview; import android. wi DGET. toast; public class galleryactivity extends activity {/** called when the activity is first created. */private gallery = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); gallery = (Gallery) findviewbyid (R. id. gallery); // sets the image adapter Gallery. setadapter (New imageadapter (this); Gallery. setspacing (5); // sets the listener g Allery. setonitemclicklistener (New onitemclicklistener () {public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {toast. maketext (galleryactivity. this, "click the" + arg2 + "image", toast. length_long ). show () ;}}) ;}} class imageadapter extends baseadapter {int mgalleryitembackground; private context; // image source array private integer [] imageinteger = {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d}; Public imageadapter (context c) {context = C; typedarray ATTR = context. obtainstyledattributes (R. styleable. hellogallery); mgalleryitembackground = ATTR. getresourceid (R. styleable. hellogallery_android_galleryitembackground, 0); ATTR. recycle ();} // obtain the number of images public int getcount () {return imageinteger. length ;}// obtain the position of the image in the library public object getitem (INT position) {return position ;}// obtain the image ID public long getitemid (INT position) {return position ;} public View getview (INT position, view convertview, viewgroup parent) {imageview = new imageview (context); // set the imageview resource for imageview. setimageresource (imageinteger [position]); // sets the display ratio type imageview. setscaletype (imageview. scaletype. fit_xy); // set the layout image 120*80 imageview. setlayoutparams (new gallery. layoutparams (180,100); imageview. setbackgroundresource (mgalleryitembackground); Return imageview ;}}
Mian. xml
<?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:orientation="vertical" ><Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="bottom" android:background="?android:galleryItemBackground"/></LinearLayout>
Create a New XML file inRes/values/directory
Name attrs. xml.
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="HelloGallery"> <attr name="android:galleryItemBackground" /> </declare-styleable></resources>
This is a customized styleable resource that can be applied to a layout.