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

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

        
  1. 代码
  2. packagecom.amaker.test;
  3. importandroid.app.Activity;
  4. importandroid.content.Context;
  5. importandroid.os.Bundle;
  6. importandroid.view.View;
  7. importandroid.view.ViewGroup;
  8. importandroid.widget.BaseAdapter;
  9. importandroid.widget.Gallery;
  10. importandroid.widget.ImageView;
  11. publicclassMainActivityextendsActivity{
  12. /**Calledwhentheactivityisfirstcreated.*/
  13. @Override
  14. //创建时被调用
  15. publicvoidonCreate(BundlesavedInstanceState){
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. Galleryg=(Gallery)findViewById(R.id.Gallery01);
  19. g.setAdapter(newMyAdapter(this));
  20. }
  21. classMyAdapterextendsBaseAdapter{
  22. Contextcontext;
  23. MyAdapter(Contextcontext){
  24. this.context=context;
  25. }
  26. privateInteger[]imgs={
  27. R.drawable.gallery_photo_1,
  28. R.drawable.gallery_photo_2,
  29. R.drawable.gallery_photo_3,
  30. R.drawable.gallery_photo_4,
  31. R.drawable.gallery_photo_5,
  32. R.drawable.gallery_photo_6,
  33. R.drawable.gallery_photo_7,
  34. R.drawable.gallery_photo_8,
  35. R.drawable.gallery_photo_1,
  36. R.drawable.gallery_photo_2,
  37. R.drawable.gallery_photo_3,
  38. R.drawable.gallery_photo_4,
  39. R.drawable.gallery_photo_5,
  40. R.drawable.gallery_photo_6,
  41. R.drawable.gallery_photo_7,
  42. R.drawable.gallery_photo_8
  43. };
  44. publicintgetCount(){
  45. returnimgs.length;
  46. }
  47. publicObjectgetItem(intitem){
  48. returnitem;
  49. }
  50. publiclonggetItemId(intid){
  51. returnid;
  52. }
  53. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  54. ImageViewimageView=newImageView(context);
  55. imageView.setImageResource(imgs[position]);
  56. imageView.setScaleType(ImageView.ScaleType.FIT_XY);
  57. imageView.setLayoutParams(newGallery.LayoutParams(136,88));
  58. returnimageView;
  59. }
  60. }
  61. }

/Chapter04_UI_Gallery01/res/layout/main.xml

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

更多相关文章

  1. Dagger-Android使用手册
  2. 整理出15个Android很有用的代码片段
  3. androidのMMS短信发送过程(一)
  4. Android从入门到放弃——常见控件二
  5. Android(安卓)发布可穿戴设备 SDK 的开发者预览版
  6. Android(安卓)常用代码集合
  7. Android(安卓)开发中遇到的 bug(6)
  8. 25个你应该阅读的Android网站
  9. android Launcher Demo小结

随机推荐

  1. 视频教程-【Android峰会】如何高效高质进
  2. Android(安卓)4.0新特性总结
  3. Android编码规范指南
  4. Android之条形码、二维码扫描框架(非原创
  5. android 工程横竖屏设置无效 cocos2dx v3
  6. Android:适配不同分辨率&hdpi、mdpi、ldpi
  7. 转:android listView 继承ListActivity的
  8. Android SDK安装Android4.0“冰激淋三明
  9. Android(安卓)系统属性的设置查询
  10. 【Android(安卓)Training - 03】使用Frag