Gallery是Android中的图片库控件。先看效果,爽一番
一、简介
在中心锁定,水平显示列表的项。
二、实例
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 > |
2.属性文件
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 | <!-- These are the attributes that we want to retrieve from the theme |
10 | in view/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 > |
3.代码
001 | /** |
002 | * |
003 | */ |
004 | package wjq.WidgetDemo; |
005 | |
006 | import android.R.layout; |
007 | import android.app.Activity; |
008 | import android.content.Context; |
009 | import android.content.res.TypedArray; |
010 | import android.database.Cursor; |
011 | import android.os.Bundle; |
012 | import android.provider.Contacts.People; |
013 | import android.view.ContextMenu; |
014 | import android.view.MenuItem; |
015 | import android.view.View; |
016 | import android.view.ViewGroup; |
017 | import android.view.ContextMenu.ContextMenuInfo; |
018 | import android.widget.BaseAdapter; |
019 | import android.widget.Gallery; |
020 | import android.widget.ImageView; |
021 | import android.widget.SimpleCursorAdapter; |
022 | import android.widget.SpinnerAdapter; |
023 | import android.widget.Toast; |
024 | import android.widget.AdapterView.AdapterContextMenuInfo; |
025 | |
026 | /** |
027 | * @author 记忆的永恒 |
028 | * |
029 | */ |
030 | public class GalleryDemo extends Activity { |
031 | private Gallery gallery; |
032 | private Gallery gallery1; |
033 | |
034 | /* |
035 | * (non-Javadoc) |
036 | * |
037 | * @see android.app.Activity#onCreate(android.os.Bundle) |
038 | */ |
039 | @Override |
040 | protected void onCreate(Bundle savedInstanceState) { |
041 | // TODO Auto-generated method stub |
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 | Cursor c = getContentResolver().query(People.CONTENT_URI, null , null , null , null ); |
050 | startManagingCursor(c); |
051 | |
052 | SpinnerAdapter adapter = new SimpleCursorAdapter( this , |
053 | // Use a template that displays a text view |
054 | android.R.layout.simple_gallery_item, |
055 | // Give the cursor to the list adatper |
056 | c, |
057 | // Map the NAME column in the people database to... |
058 | new String[] {People.NAME}, |
059 | // The "text1" view defined in the XML template |
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(ContextMenu menu, View v, |
068 | ContextMenuInfo menuInfo) { |
069 | menu.add( "Action" ); |
070 | } |
071 | |
072 | @Override |
073 | public boolean onContextItemSelected(MenuItem item) { |
074 | AdapterContextMenuInfo info = (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 Context mContext; |
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(Context context) { |
089 | mContext = context; |
090 | |
091 | TypedArray a = 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 Object getItem( int position) { |
104 | return position; |
105 | } |
106 | |
107 | @Override |
108 | public long getItemId( int position) { |
109 | return position; |
110 | } |
111 | |
112 | @Override |
113 | public View getView( int position, View convertView, ViewGroup parent) { |
114 | ImageView i = 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 | // The preferred Gallery item background |
121 | i.setBackgroundResource(mGalleryItemBackground); |
122 | |
123 | return i; |
124 | } |
125 | |
126 | } |
127 | |
128 | |
129 | } |