标签:actionbar android android应用 popupwindow 弹窗
MainActivity.java
|
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
|
public class MainActivity extends Activity
{ private TextView
tv_pop; private ListView
group_list; private TextView
tv_group_item; private View
view; private PopupWindow
pop; private List<String>
list; protected void onCreate(Bundle
savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.title); tv_pop
= (TextView) findViewById(R.id.tv_pop); tv_group_item
= (TextView) findViewById(R.id.tv_group_item); tv_pop.setText("学期"); tv_pop.setOnClickListener(new View.OnClickListener()
{ public void onClick(View
v) { showWindow(v); } }); } protected void showWindow(View
v) { if (pop
== null)
{ LayoutInflater
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); //将listView的布局文件加载到View中 view
= layoutInflater.inflate(R.layout.grou_list, null); group_list=(ListView)
view.findViewById(R.id.group_list); //List数组 list
= new ArrayList<String>(); list.add("2012-2013学年第1学期"); list.add("2012-2013学年第2学期"); list.add("2013-2014学年第1学期"); list.add("2013-2014学年第2学期"); GroupAdapter
groupAdapter = new GroupAdapter(this,
list); group_list.setAdapter(groupAdapter); // pop
= new PopupWindow(view,300, 250); } //
使其得到聚焦 pop.setFocusable(true); //
设置允许点击外部的地方消失 pop.setOutsideTouchable(true); //
设置点击返回键也能使其消失,并且不会影响你的背景 pop.setBackgroundDrawable(new BitmapDrawable()); //获取屏幕的宽度,计算弹窗弹出位置的x坐标 WindowManager
windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); int xPos
= windowManager.getDefaultDisplay().getWidth() / 2 -
pop.getWidth() / 2; //设置弹窗弹出的位置 pop.showAsDropDown(v,
-100,0);//
pop.showAtLocation(v, Gravity.TOP, 0,45); //设置List点击后的点击事件 group_list.setOnItemClickListener(new OnItemClickListener()
{ public void onItemClick(AdapterView<?>
adapterView, View viewe, int position, long id)
{ Toast.makeText(MainActivity.this,
list.get(position), Toast.LENGTH_SHORT).show(); if (pop
!= null)
{ pop.dismiss(); } } }); }} |
GroupAdapter:
|
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
|
public class GroupAdapter extends BaseAdapter
{ private Context
context; private List<String>
list; public GroupAdapter(Context
context, List<String> list) { this.context
= context; this.list
= list; } public int getCount()
{ return list.size(); } public Object
getItem(int position)
{ return list.get(position); } public long getItemId(int position)
{ return position; } public View
getView(int position,
View convertView, ViewGroup parent) { ViewHolder
holder; if (convertView
== null)
{ convertView
= LayoutInflater.from(context).inflate( R.layout.group_item, null); holder
= new ViewHolder(); convertView.setTag(holder); holder.groupItem
= (TextView) convertView .findViewById(R.id.tv_group_item); } else { holder
= (ViewHolder) convertView.getTag(); } holder.groupItem.setText(list.get(position)); return convertView; } static class ViewHolder
{ TextView
groupItem; }} |
activity_main.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
android:layout_width="match_parent" android:layout_height="500dp" android:background="#D2D2D5" android:orientation="vertical" > <Button android:id="@+id/bb" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="button" /></LinearLayout> |
title.xml
自定义标题栏的标题栏的布局文件
|
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
|
android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#E1E6F6" > <TextView android:id="@+id/tv_pop" android:layout_width="wrap_content" android:layout_height="45dip" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:gravity="center" android:text="学期" android:textSize="23dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="44dip" android:src="@drawable/title_right" /> <ImageView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:src="@drawable/title_left" /></RelativeLayout> |
group_list.xml
弹窗中的listview的布局文件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version="1.0" encoding="utf-8"?> android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="0.0sp" android:paddingRight="0.0sp" android:layout_margin="0.0px" android:background="@drawable/group_bg" > <ListView android:id="@+id/group_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="true" android:cacheColorHint="#00000000" android:dividerHeight="2.0px" ></ListView></LinearLayout> |
group_item.xml
设置弹窗中ListView中加载的控件textview
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?xml version="1.0" encoding="utf-8"?> android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_group_item" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" /></LinearLayout> |
效果图:

标签:actionbar android android应用 popupwindow 弹窗
原文地址:http://blog.csdn.net/itwuchen/article/details/42340503