Gallery and Spinner have a common parent class: Abspinner, which shows that gallery and spinner are both a list box. The difference between them is that spinner displays a vertical list selection box, and gallery displays a horizontal list selection box. The role of spinner is for the user to choose, while Gallery allows the user to drag to view the previous, next.
Gallery usage is similar to spinner usage, as long as it provides a content adapter. The GetView method of adapter Returns the list item for the view as a gallery list. If the program needs to monitor changes to the gallery selection, you can add Onitemselectedlistener listening.
XML attributes for Gallery
Here's a slideshow example to familiarize yourself with gallery
(1) Activity_main.xml layout a imageswitcher and gallery imageswitcher for displaying gallery selected pictures
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <imageswitcher Android:id= "@+id/imgswt" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"/> <gallery android:id= "@+id/gallery" android:layout_width= "fill_parent" android:layout_ height= "Wrap_content" android:layout_margintop= "25DP" android:unselectedalpha= "0.7" android: spacing= "2pt"/></linearlayout>
(2) Mainactivity.java
Package Com.example.gallery;import Android.app.activity;import Android.os.bundle;import android.view.Menu;import Android.view.view;import Android.view.viewgroup;import Android.view.animation.animationutils;import Android.widget.adapterview;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.baseadapter;import Android.widget.gallery;import Android.widget.imageswitcher;import Android.widget.imageview;import Android.widget.radiogroup.layoutparams;import Android.widget.viewswitcher.viewfactory;public class Mainactivity extends Activity {//define component private Imageswitcher IMGSWT = null;private Gallery Gallery = null, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Get layout component IMGSWT = (imageswitcher) Findviewbyid ( R.ID.IMGSWT) Gallery = (gallery) Findviewbyid (r.id.gallery);//Picture final int images[] = new INT[]{R.DRAWABLE.NAME01, R.drawable.name02,r.drawable.name03,r.drawable.name04,r.drawable.naMe05,r.drawable.name06,r.drawable.name07,r.drawable.name08,r.drawable.name09,r.drawable.name10, r.drawable.name11,r.drawable.name12};//Set Picture Toggle Effect Imgswt.setinanimation (Animationutils.loadanimation (This, Android . r.anim.fade_in)); Imgswt.setoutanimation (Animationutils.loadanimation (this, Android. r.anim.fade_out));//Set Viewfactory object Imgswt.setfactory (New Viewfactory () {@Overridepublic View Makeview () {ImageView ImageView = new ImageView (mainactivity.this); Imageview.setscaletype (ImageView.ScaleType.FIT_CENTER); Imageview.setlayoutparams (New Imageswitcher.layoutparams (350,350)); return imageView;}); /Create Baseadapter object, responsible for providing gallery display all images baseadapter adapter = new Baseadapter () {@Overridepublic View getView (int position, View Convertview, ViewGroup parent) {//create Imageviewimageview ImageView = new ImageView (mainactivity.this); Imageview.setimageresource (Images[position]); Imageview.setscaletype (ImageView.ScaleType.FIT_XY); Imageview.setlayoutparams (New Gallery.layoutparams (Layoutparams.wrap_content,layoutparaMs. wrap_content)); return ImageView;} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic Object getItem (int position) {return position;} @Overridepublic int GetCount () {return images.length;}};/ /set Adapter Gallery.setadapter (adapter) to Gallery;//Add Event Gallery.setonitemselectedlistener (new Onitemselectedlistener () { @Overridepublic void onitemselected (adapterview<?> parent, View view,int position, long ID) { Imgswt.setimageresource (Images[position]);} @Overridepublic void onnothingselected (adapterview<?> parent) {//TODO auto-generated Method stub}});}}
The results are as follows: