1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
public
class
MainActivity
extends
Activity
{
/*
* 使用Gallery时,xml记得配置图片间隔
* android:spacing="16dp"
*/
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(
new
BaseAdapter()
{
@Override
public
View getView(
int
position, View convertView, ViewGroup parent)
{
LinearLayout lilayout =
null
;
if
(convertView ==
null
)
{
LayoutInflater inflater = getLayoutInflater();
lilayout = (LinearLayout) inflater.inflate(R.layout.relayout,
null
);
}
else
{
lilayout = (LinearLayout) convertView;
}
ImageView imageView = (ImageView) lilayout.findViewById(R.id.imageView1);
TextView textView = (TextView) lilayout.findViewById(R.id.textView1);
imageView.setImageResource(mImageIds[position]);
textView.setText(mImageNames[position]);
return
lilayout;
}
@Override
public
long
getItemId(
int
position)
{
return
0
;
}
@Override
public
Object getItem(
int
position)
{
return
null
;
}
@Override
public
int
getCount()
{
return
mImageIds.length;
}
});
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
private
Integer[] mImageIds = {
R.drawable.gallery_photo_1,
R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3,
R.drawable.gallery_photo_4,
R.drawable.gallery_photo_5,
R.drawable.gallery_photo_6,
R.drawable.gallery_photo_7,
R.drawable.gallery_photo_8
};
private
String[] mImageNames = {
"photo_1"
,
"photo_2"
,
"photo_3"
,
"photo_4"
,
"photo_5"
,
"photo_6"
,
"photo_7"
,
"photo_8"
};
}
|
注:
* 使用Gallery时,xml记得配置图片间隔
* android:spacing="16dp"
系统范例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
public
class
Gallery1
extends
Activity
{
@Override
public
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.gallery_1);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(
new
ImageAdapter(
this
));
// 点击弹出提示
g.setOnItemClickListener(
new
OnItemClickListener()
{
public
void
onItemClick(AdapterView parent, View v,
int
position,
long
id)
{
Toast.makeText(Gallery1.
this
,
""
+ position, Toast.LENGTH_SHORT)
.show();
}
});
registerForContextMenu(g);
// 长按图片弹出对话框
}
@Override
public
void
onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
menu.add(R.string.gallery_2_text);
}
@Override
public
boolean
onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
Toast.makeText(
this
,
"Longpress: "
+ info.position, Toast.LENGTH_SHORT)
.show();
return
true
;
}
public
class
ImageAdapter
extends
BaseAdapter
{
int
mGalleryItemBackground;
public
ImageAdapter(Context c)
{
mContext = c;
// See res/values/attrs.xml for the <declare-styleable> that defines
// Gallery1.
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItem a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground,
0
);
a.recycle();
}
public
int
getCount()
{
return
mImageIds.length;
}
public
Object getItem(
int
position)
{
return
position;
}
public
long
getItemId(
int
position)
{
return
position;
}
public
View getView(
int
position, View convertView, ViewGroup parent)
{
ImageView i =
new
ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(
new
Gallery.LayoutParams(
136
,
88
));
// The preferred Gallery item background
i.setBackgroundResource(mGalleryItemBackground);
return
i;
}
private
Context mContext;
private
Integer[] mImageIds =
{ R.drawable.gallery_photo_1, R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3, R.drawable.gallery_photo_4,
R.drawable.gallery_photo_5, R.drawable.gallery_photo_6,
R.drawable.gallery_photo_7, R.drawable.gallery_photo_8 };
}
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1202974,如需转载请自行联系原作者