mainActiviy精选图书ppackage com.example.mygallary; 导入安卓. app.activity; 导入安卓. content.context; 导入安卓. content.RES.typed array; 导入安卓. OS.bundle; 导入安卓. view.menu; 导入安卓. view.view; 导入Android.view.view group; 导入Android .构件. base adapter; import Android .构件. gallery; import Android .构件. imageview; publicclassmainactivityextendsactivity { gallery gal; @ overridepublicvoidoncreate (bundlesavedinstancestate ) super.oncreate ) savedinstancestate; setcontentview (r.layout.activity _ main; imageadapteradapter=new image adapter (this; gal=(gallery ) findviewbyid ) r.id.gal; gal.set adapter (适配器); gal.set selection (integer.max _ value/2 ); } publicclassimageadapterextendsbaseadapter { context context=null; int mGalleryItemBackground; publicimageadapter (上下文上下文) { this.context=context; typedarraya=obtainstyledattributes (r.style able.hello gallery ); //资源文件mgalleryitembackground=a.get resourceid (r.style able.hello gallery _ Android _ galleryitembackground,0 设置Gallery的背景布局a.recycle (; } @ overridepublicintgetcount ((/todo auto-generatedmethodstubreturninteger.max _ value; }int ids[]={R.drawable.s1,R.drawable.s2,R.drawable.s3,R.drawable.s4,R.drawable.s5,r } r.drawable @ overridepublicobjectgetitem (int position )//todo auto-generatedmethodstubreturnids [位置%6]; } @ overridepubliclonggetitemid (int position )/todo auto-generatedmethodstubreturnposition % 6; } @ overridepublicviewgetview (int position,tldjj,ViewGroup parent ) {ImageView imageView=null; if (转换视图==空) imageview=new imageview (上下文); 转换视图=imageview; } imageview.setimageresource (ids [位置%6]; imageview.setbackgroundresource (mgalleryitembackground; imageview.setlayoutparams (new gallery.layout params (260,220 ) ); imageview.setscaletype (imageview.scale type.fit _ xy; 返回图像视图; } @ overridepublicbooleanoncreateoptionsmenu (menu menu ) { getMenuInflater ).inflate } r.menu.activity _ main,met menu }}values包下的attr.xml
? XML版本=’ 1.0 ‘编码=’ utf-8 ‘? 资源描述符- styleablename=’ hello gallery ‘ attrname=’ Android : galleryitembackground ‘/declare-style able /
drawable软件包中有6张图像S1 S2 .