vGallery:和Spinner都继承自AbsSpinner,这说明GallerySpinner都是一个列表框。它们之间的区别在于Spinner显示的是一个垂直的列表选择框,而Gallery显示的是一个水平的列表框。
vSpinner的作用是供用户选择,而Gallery则允许用户通过拖动来查看上一个、下一个列表项。
v用法相似,只要提供一个内容Adapter即可。

 

 


  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <Gallery   
  8.         android:id="@+id/gallery_view" 
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="wrap_content" 
  11.         android:unselectedAlpha="0.5" 
  12.         android:spacing="3pt"/> 
  13.  
  14.     <ImageView   
  15.         android:id="@+id/gallery_p_w_picpath" 
  16.         android:layout_width="280dp" 
  17.         android:layout_height="280dp" 
  18.         android:layout_gravity="center_horizontal"/> 
  19. </LinearLayout> 

需要定义一个类,继承BaseAdapter,应为它继承了SpinnerAdapter


  1. import android.app.Activity;  
  2. import android.content.Context;  
  3. import android.content.res.TypedArray;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.AdapterView;  
  8. import android.widget.BaseAdapter;  
  9. import android.widget.Gallery;  
  10. import android.widget.ImageView;  
  11. import android.widget.AdapterView.OnItemSelectedListener;  
  12.  
  13. public class GalleryDemo extends Activity{  
  14.     p_w_picpathadapter ida;  
  15.     @Override 
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.gallery);  
  19.           
  20.         Gallery gallery = (Gallery) findViewById(R.id.gallery_view);  
  21.         ida=new p_w_picpathadapter(this);  
  22.         gallery.setAdapter(ida);  
  23.           
  24.         gallery.setOnItemSelectedListener(new OnItemSelectedListener() {  
  25.  
  26.             @Override 
  27.             public void onItemSelected(AdapterView<?> parent, View view,  
  28.                     int position, long id) {  
  29.                 ImageView p_w_picpathView=(ImageView) findViewById(R.id.gallery_p_w_picpath);  
  30.                 p_w_picpathView.setImageResource(ida.mImageIds[position]);  
  31.             }  
  32.  
  33.             @Override 
  34.             public void onNothingSelected(AdapterView<?> parent) {  
  35.                   
  36.             }  
  37.         });  
  38.           
  39.     }  
  40.  
  41. }  
  42. //********************************************************************************//  
  43. //重写BaseAdapter方法,它继承了SpinnerAdapter   
  44. class p_w_picpathadapter extends BaseAdapter{  
  45.     //定义数组,加入图片id  
  46.     public int[] mImageIds=new int[]{  
  47.             R.drawable.jinniu,R.drawable.chunv,R.drawable.juxie,R.drawable.mojie,  
  48.             R.drawable.sheshou,R.drawable.shizi,R.drawable.shuangyu,R.drawable.shuangzi,  
  49.             R.drawable.shuiping,R.drawable.tiancheng,R.drawable.tianxie,R.drawable.baiyang  
  50.     };  
  51.     int mGalleryItemBackground;      
  52.     private Context mContext;  
  53.     //有参构造,参数是context:指向对象  
  54.     public  p_w_picpathadapter(Context c){  
  55.         mContext =c;  
  56.         TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);          
  57.         mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);          
  58.         attr.recycle();  
  59.     }  
  60.     @Override 
  61.     public int getCount() {  
  62.         return mImageIds.length;  
  63.     }  
  64.  
  65.     @Override 
  66.     public Object getItem(int position) {  
  67.         return position;  
  68.     }  
  69.  
  70.     @Override 
  71.     public long getItemId(int position) {  
  72.         return position;  
  73.     }  
  74.  
  75.     // 该方法的返回的View就是代表了每个列表项  
  76.     @Override 
  77.     public View getView(int position, View convertView, ViewGroup parent) {  
  78.         ImageView p_w_picpathView = new ImageView(mContext);        //创建一个p_w_picpathview  
  79.         p_w_picpathView.setImageResource(mImageIds[position]);        //给p_w_picpathview添加图片  
  80.         p_w_picpathView.setLayoutParams(new Gallery.LayoutParams(150100));   //设置布局参数       
  81.         p_w_picpathView.setScaleType(ImageView.ScaleType.FIT_XY);       //设置类型适合xy   
  82.         p_w_picpathView.setBackgroundResource(mGalleryItemBackground);   //设置背景       
  83.         return p_w_picpathView;  
  84.     }  
  85.       
  86.       

 

GalleryDemo幻灯片