(4.1.9)gallery

来源:互联网 发布:tcpip网络层协议有哪些 编辑:程序博客网 时间:2024/06/07 20:47
  运行效果如下:

选择一张好看的后,效果如下

activity_main.xml 代码:

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.     <ImageButton   
  6.         android:id="@+id/myimagebutton"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:src="@drawable/ic_launcher"  
  10.         />  
  11.     <EditText   
  12.         android:layout_width="200dp"  
  13.         android:layout_height="wrap_content"  
  14.         />  
  15. </LinearLayout>  


弹框的View布局文件代码为:

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"   
  5.     android:orientation="vertical"  
  6.     >  
  7.     <Gallery  
  8.         android:id="@+id/mygallery"  
  9.         android:layout_width="220dp"  
  10.         android:layout_height="60dp"  
  11.         android:spacing="10dp" />  
  12. </LinearLayout>  


Gallery的item布局文件为:

[html] view plaincopy
  1. <LinearLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical">  
  6.     <ImageView   
  7.         android:id="@+id/image"  
  8.         android:layout_gravity="center_horizontal"  
  9.         android:layout_width="40dp"  
  10.         android:layout_height="40dp"/>  
  11.     <TextView   
  12.         android:id="@+id/text"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_gravity="center_horizontal"  
  16.         android:textSize="8dp"  
  17.         android:gravity="center"  
  18.         android:textColor="#ffffffff"/>  
  19. </LinearLayout>  


Activity代码为:

[java] view plaincopy
  1. public class MainActivity extends Activity implements OnItemSelectedListener{  
  2.       
  3.     Gallery gallery;  
  4.     int [] smalldrawable = new int[]{R.drawable.sample_thumb_0,R.drawable.sample_thumb_1,R.drawable.sample_thumb_2,R.drawable.sample_thumb_3,R.drawable.sample_thumb_4,R.drawable.sample_thumb_5,R.drawable.sample_thumb_6,R.drawable.sample_thumb_7};  
  5.       
  6.     int position = 0;  
  7.     ImageButton imageButton;  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.           
  13.           
  14.         AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  15.         LayoutInflater layoutInflater = LayoutInflater.from(this);  
  16.         final View view = layoutInflater.inflate(R.layout.mydialog, null);  
  17.         builder.setTitle("请选择一张好看的");  
  18.         builder.setView(view);  
  19.           
  20.           
  21.         gallery = (Gallery) view.findViewById(R.id.mygallery);  //此处的findViewById必须要加上view.  
  22.         gallery.setAdapter(new ImageAdapter(this));  
  23.         gallery.setSelection(0);  
  24.         gallery.setOnItemSelectedListener(this);  
  25.           
  26.           
  27.         imageButton = (ImageButton) findViewById(R.id.myimagebutton);  
  28.           
  29.         builder.setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  30.             @Override  
  31.             public void onClick(DialogInterface dialog, int which) {  
  32.                 System.out.println(position);  
  33.                 imageButton.setImageResource(smalldrawable[position]);  
  34.             }  
  35.         });  
  36.           
  37.         builder.setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  38.             @Override  
  39.             public void onClick(DialogInterface dialog, int which) {  
  40.             }  
  41.         });       
  42.         builder.create().show();  
  43.           
  44.           
  45.     }  
  46.   
  47.     private class ImageAdapter extends BaseAdapter{  
  48.         Context context;  
  49.           
  50.         String [] str = new String[]{"第一张","第二张","第三张","第四张","第五张","第六张","第七张","第八张"};  
  51.         public ImageAdapter(Context context) {  
  52.             this.context = context;  
  53.         }  
  54.           
  55.         //返回图片的个数  
  56.         @Override  
  57.         public int getCount() {  
  58.             return smalldrawable.length;  
  59.         }  
  60.   
  61.         @Override  
  62.         public Object getItem(int position) {  
  63.             return smalldrawable[position];  
  64.         }  
  65.   
  66.         @Override  
  67.         public long getItemId(int position) {  
  68.             return position;  
  69.         }  
  70.   
  71.         @Override  
  72.         public View getView(int position, View convertView, ViewGroup parent) {  
  73.             ViewHolder holder;  
  74.             if(convertView == null){  
  75.                 //View的findViewById()方法也是比较耗时的,因此需要考虑中调用一次,之后用  
  76.                 //View的getTag()来获取这个ViewHolder对象  
  77.                 holder = new ViewHolder();  
  78.                 convertView = View.inflate(context, R.layout.mydialogtype, null);  
  79.                 holder.imageView = (ImageView) convertView.findViewById(R.id.image);  
  80.                 holder.textView = (TextView) convertView.findViewById(R.id.text);  
  81.                 convertView.setTag(holder);  
  82.             }else {  
  83.                 holder = (ViewHolder) convertView.getTag();  
  84.             }  
  85.             holder.imageView.setImageResource(smalldrawable[position]);  
  86.             holder.textView.setText(str[position]);  
  87.               
  88.             return convertView;  
  89.         }  
  90.     }  
  91.       
  92.     //增加这样一个静态类,缓存一下,这样不用每次都重新加载  
  93.     final class ViewHolder{  
  94.         public ImageView imageView;  
  95.         public TextView textView;  
  96.     }  
  97.     @Override  
  98.     public void onItemSelected(AdapterView<?> parent, View view, int position,  
  99.             long id) {  
  100.         int x = 0;  
  101.         System.out.println(position);  
  102.         this.position = position;  
  103.     }  
  104.   
  105.     @Override  
  106.     public void onNothingSelected(AdapterView<?> parent) {  
  107.     }  
  108. }  


ok,就这些了。

0 0
原创粉丝点击