android用户界面-组件Widget-画廊视图Gallery

Android的Gallery控件是个很不错的看图控件,大大减轻了开发者对于看图功能的开发,而且效果也比较美观。

 

/Chapter04_UI_Gallery01/src/com/amaker/test/MainActivity.java

 

 
 
  1. 代码  
  2.  
  3. package com.amaker.test;  
  4.  
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.BaseAdapter;  
  11. import android.widget.Gallery;  
  12. import android.widget.ImageView;  
  13.  
  14. public class MainActivity extends Activity {  
  15.     /** Called when the activity is first created. */ 
  16.     @Override 
  17.     //创建时被调用  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.         Gallery g = (Gallery) findViewById(R.id.Gallery01);  
  22.           
  23.         g.setAdapter(new MyAdapter(this));  
  24.     }  
  25.       
  26.     class MyAdapter extends BaseAdapter{  
  27.         Context context;  
  28.         MyAdapter(Context context){  
  29.             this.context = context;  
  30.         }  
  31.         private Integer[] imgs = {  
  32.                 R.drawable.gallery_photo_1,  
  33.                 R.drawable.gallery_photo_2,  
  34.                 R.drawable.gallery_photo_3,  
  35.                 R.drawable.gallery_photo_4,  
  36.                 R.drawable.gallery_photo_5,  
  37.                 R.drawable.gallery_photo_6,  
  38.                 R.drawable.gallery_photo_7,  
  39.                 R.drawable.gallery_photo_8,  
  40.                   
  41.                 R.drawable.gallery_photo_1,  
  42.                 R.drawable.gallery_photo_2,  
  43.                 R.drawable.gallery_photo_3,  
  44.                 R.drawable.gallery_photo_4,  
  45.                 R.drawable.gallery_photo_5,  
  46.                 R.drawable.gallery_photo_6,  
  47.                 R.drawable.gallery_photo_7,  
  48.                 R.drawable.gallery_photo_8  
  49.         };  
  50.           
  51.         public int getCount() {  
  52.             return imgs.length;  
  53.         }  
  54.  
  55.         public Object getItem(int item) {  
  56.             return item;  
  57.         }  
  58.  
  59.         public long getItemId(int id) {  
  60.             return id;  
  61.         }  
  62.  
  63.         public View getView(int position, View convertView, ViewGroup parent) {  
  64.              ImageView imageView = new ImageView(context);  
  65.              imageView.setImageResource(imgs[position]);  
  66.              imageView.setScaleType(ImageView.ScaleType.FIT_XY);  
  67.              imageView.setLayoutParams(new Gallery.LayoutParams(13688));  
  68.                   
  69.              return imageView;  
  70.         }  
  71.           
  72.     }  

/Chapter04_UI_Gallery01/res/layout/main.xml

 

 
 
  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  5.     android:orientation="vertical" 
  6.     android:layout_width="fill_parent" 
  7.     android:layout_height="fill_parent" 
  8.     > 
  9.  
  10. <Gallery   
  11. android:id="@+id/Gallery01"   
  12. android:layout_width="wrap_content"   
  13. android:layout_height="wrap_content"> 
  14. </Gallery> 
  15.  
  16. </LinearLayout> 

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1080689


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值