首页 > 代码库 > PopupWindow选项弹窗
PopupWindow选项弹窗
PopupWindow选项弹窗
一、popupwindow弹窗简介
popupwindow是一种弹窗在应用中经常使用,像超级课程表的周数选择,微博的标题栏的titlebar中的选择等等 ,如图所示。
但是和alertDialog很相似,但是还是有很大的区别的。AlterDialog是非阻塞式对话框:AlaterDialog弹出时,后台还可以做事情;而且PopuWindow是阻塞式对话框:PopupWindow弹出时,后台程序会等待,在PopupWindow退出之前一直处于等待,直到当我们调用了dismiss方法之后,PopuWindow退出了,程序才会向下执行。
二、PopupWindow的使用
1、构造一个PopupWindow对象。PopupWindow pop = new PopupWindow(view,300, 250);
View是加载的布局文件View view = layoutInflater.inflate(R.layout.grou_list, null);
X,Y是PopupWindow弹窗的长和宽
2、调用showAsDropDown (View anchor, int xoff, int yoff)方法.
View是点击弹出PopupWindow的View x,y是设定PopupWindow弹出的位置
也可以使用 showAtLocation (View parent, int gravity, int x, int y)方法设置弹出的位置。
3、Dismiss()方法设置PopupWindow的消失。
4、还有几个常用的设置方法。
setFocusable(true)设置使PopupWindow聚焦
setOutsideTouchable(true)设置允许点击外部的地方消失
setBackgroundDrawable(new BitmapDrawable())设置点击返回键也能使其消失。
三、例子实现
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 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" 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 | < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" 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" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" 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" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" 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 > |
效果图:
PopupWindow选项弹窗
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。