Android 控件之Gallery图片集
2012-10-18 14:24
218 查看
Gallery是Android中的图片库控件。先看效果,爽一番
widgetdemo.rar
一、简介
在中心锁定,水平显示列表的项。
二、实例
1.布局文件
查看源码
打印?
2.属性文件
查看源码
打印?
3.代码
查看源码
打印?
一、简介
在中心锁定,水平显示列表的项。
二、实例
1.布局文件
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout |
03 | xmlns:android = "http://schemas.android.com/apk/res/android" |
04 | android:layout_width = "fill_parent" |
05 | android:orientation = "vertical" |
06 | android:layout_height = "wrap_content" > |
07 |
08 | < Gallery xmlns:android = "http://schemas.android.com/apk/res/android" android:id = "@+id/gallery" |
09 | android:layout_width = "match_parent" |
10 | android:layout_height = "wrap_content" |
11 | /> |
12 |
13 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
14 | android:orientation = "vertical" |
15 | android:layout_width = "match_parent" |
16 | android:layout_height = "wrap_content" > |
17 |
18 | < Gallery android:id = "@+id/gallery1" |
19 | android:layout_width = "match_parent" |
20 | android:layout_height = "wrap_content" |
21 | android:gravity = "center_vertical" |
22 | android:spacing = "16dp" |
23 | /> |
24 |
25 | </ LinearLayout > |
26 |
27 |
28 | </ LinearLayout > |
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 |
03 | < resources > |
04 |
05 | < declare-styleable name = "TogglePrefAttrs" > |
06 | < attr name = "android:preferenceLayoutChild" /> |
07 | </ declare-styleable > |
08 |
09 | <!--Thesearetheattributesthatwewanttoretrievefromthetheme |
10 | inview/Gallery1.java--> |
11 | < declare-styleable name = "Gallery1" > |
12 | < attr name = "android:galleryItemBackground" /> |
13 | </ declare-styleable > |
14 |
15 | < declare-styleable name = "LabelView" > |
16 | < attr name = "text" format = "string" /> |
17 | < attr name = "textColor" format = "color" /> |
18 | < attr name = "textSize" format = "dimension" /> |
19 | </ declare-styleable > |
20 | </ resources > |
001 | /** |
002 | * |
003 | */ |
004 | package
|
005 |
006 | import
|
007 | import
|
008 | import
|
009 | import
|
010 | import
|
011 | import
|
012 | import
|
013 | import
|
014 | import
|
015 | import
|
016 | import
|
017 | import
|
018 | import
|
019 | import
|
020 | import
|
021 | import
|
022 | import
|
023 | import
|
024 | import
|
025 |
026 | /** |
027 | *@author记忆的永恒 |
028 | * |
029 | */ |
030 | public class GalleryDemo extends Activity{ |
031 | private Gallerygallery; |
032 | private Gallerygallery1; |
033 |
034 | /* |
035 | *(non-Javadoc) |
036 | * |
037 | *@seeandroid.app.Activity#onCreate(android.os.Bundle) |
038 | */ |
039 | @Override |
040 | protected void onCreate(BundlesavedInstanceState){ |
041 | //TODOAuto-generatedmethodstub |
042 | super .onCreate(savedInstanceState); |
043 | setContentView(R.layout.gallerypage); |
044 | gallery=(Gallery)findViewById(R.id.gallery); |
045 | gallery.setAdapter( new ImageAdapter( this )); |
046 |
047 | registerForContextMenu(gallery); |
048 |
049 | Cursorc=getContentResolver().query(People.CONTENT_URI, null , null , null , null ); |
050 | startManagingCursor(c); |
051 |
052 | SpinnerAdapteradapter= new SimpleCursorAdapter( this , |
053 | //Useatemplatethatdisplaysatextview |
054 | android.R.layout.simple_gallery_item, |
055 | //Givethecursortothelistadatper |
056 | c, |
057 | //MaptheNAMEcolumninthepeopledatabaseto... |
058 | new String[]{People.NAME}, |
059 | //The"text1"viewdefinedintheXMLtemplate |
060 | new int []{android.R.id.text1}); |
061 |
062 | gallery1=(Gallery)findViewById(R.id.gallery1); |
063 | gallery1.setAdapter(adapter); |
064 | } |
065 |
066 | @Override |
067 | public void onCreateContextMenu(ContextMenumenu,Viewv, |
068 | ContextMenuInfomenuInfo){ |
069 | menu.add( "Action" ); |
070 | } |
071 |
072 | @Override |
073 | public boolean onContextItemSelected(MenuItemitem){ |
074 | AdapterContextMenuInfoinfo=(AdapterContextMenuInfo)item |
075 | .getMenuInfo(); |
076 | Toast.makeText( this , "Longpress:" +info.position,Toast.LENGTH_SHORT) |
077 | .show(); |
078 | return true ; |
079 | } |
080 |
081 | public class ImageAdapter extends BaseAdapter{ |
082 | int mGalleryItemBackground; |
083 | private ContextmContext; |
084 |
085 | private Integer[]mImageIds={R.drawable.b,R.drawable.c, |
086 | R.drawable.d,R.drawable.f,R.drawable.g}; |
087 |
088 | public ImageAdapter(Contextcontext){ |
089 | mContext=context; |
090 |
091 | TypedArraya=obtainStyledAttributes(R.styleable.Gallery1); |
092 | mGalleryItemBackground=a.getResourceId( |
093 | R.styleable.Gallery1_android_galleryItemBackground, 0 ); |
094 | a.recycle(); |
095 | } |
096 |
097 | @Override |
098 | public int getCount(){ |
099 | return mImageIds.length; |
100 | } |
101 |
102 | @Override |
103 | public ObjectgetItem( int position){ |
104 | return position; |
105 | } |
106 |
107 | @Override |
108 | public long getItemId( int position){ |
109 | return position; |
110 | } |
111 |
112 | @Override |
113 | public ViewgetView( int position,ViewconvertView,ViewGroupparent){ |
114 | ImageViewi= new ImageView(mContext); |
115 |
116 | i.setImageResource(mImageIds[position]); |
117 | i.setScaleType(ImageView.ScaleType.FIT_XY); |
118 | i.setLayoutParams( new Gallery.LayoutParams( 300 , 400 )); |
119 |
120 | //ThepreferredGalleryitembackground |
121 | i.setBackgroundResource(mGalleryItemBackground); |
122 |
123 | return i; |
124 | } |
125 |
126 | } |
127 |
128 |
129 | } |
相关文章推荐
- Android 控件之Gallery图片集
- Android 控件之Gallery图片集
- Android 控件之Gallery图片集
- Android 控件之Gallery图片集
- Android Gallery控件使用方法详解
- 系出名门Android(8) - 控件(View)之TextSwitcher, Gallery, ImageSwitcher, GridView, ListVi
- ios 仿android gallery控件
- Android Gallery控件
- Android常用控件之HorizontalScrollView代替Gallery
- Android UI控件之Gallery(拖动效果) --拖动式图片浏览
- Android控件开发之Gallery
- Android 的gallery控件
- Android控件Gallery3D效果
- Android Gallery控件使用方法详解
- android gallery 画廊控件示例
- Android UI控件之Gallery(拖动效果) --拖动式图片浏览
- Android---被弃用的Gallery控件+ImageSwitcher控件基础
- Android控件开发之Gallery3D效果
- Android控件系列之相册Gallery&Adapter适配器入门&控件缩放动画
- android gallery控件的使用问题