本文主要介绍PopupWindow的基本知识、利用PopupWindow实现类似网页上或者windows开始按钮的菜单效果以及如何解决PopupWindow和listView或GridView同时使用时焦点及页面响应问题
因为PopupWindow能实现非模态对话框效果,所以建议大家使用,而不是用AlertDialog等模态对话框阻止用户的操作.
1、PopupWindow介绍
PopupWindow可以用来装载一些信息或是View,它可以悬浮在当前活动窗口上,并且不干扰用户对背后窗口的操作。
1.1 PopupWindow创建方式,主要为
a. 显示contentView
PopupWindow(View contentView)
b. 显示contentView,固定的高度和宽度
PopupWindow(View contentView, int width, int height)
更多见PopupWindow
1.2 PopupWindow显示方式,三种分别为
a. 在anchor的左下角显示PopupWindow,偏移位置为xoff和yoff,即相当于以anchor的左下角定点为二维坐标系的原点,向右为正x,向下为正y
void showAsDropDown(View anchor, int xoff, int yoff)
b. 在anchor的左下角显示PopupWindow,偏移位置为0
void showAsDropDown(View anchor)
c. 在固定位置显示,parent为父view,gravity为显示的格式(如剧中),x和y为坐标点
void showAtLocation(View parent, int gravity, int x, int y)
1.3 其他可能用到的
焦点,设置PopupWindow是否可以获得焦点。
输入法模式setInputMethodMode。有三种为允许输入法、不允许输入法、根据是否可以有焦点决定。
2、利用PopupWindow实现菜单效果
2.1 利用listView显示
// list的数据源 List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map1 = new HashMap<String, String>(); map1.put("menuName", "菜单1"); HashMap<String, String> map2 = new HashMap<String, String>(); map2.put("menuName", "菜单2"); HashMap<String, String> map3 = new HashMap<String, String>(); map3.put("menuName", "菜单3"); list.add(map1); list.add(map2); list.add(map3); SimpleAdapter listAdapter = new SimpleAdapter(AccountManageActivity.this, list, R.layout.account_manage_long_click_row, new String[] {PopupWindowMenu.FIRST_COLUMN}, new int[] {R.id.accountLongClickTextView}); View view = getLayoutInflater().inflate(R.layout.account_manage_list, null); ListView listView = (ListView)view.findViewById(R.id.listView); listView.setAdapter(listAdapter); // PopupWindow定义,显示view,以及初始化长和宽 PopupWindow menu = new PopupWindow(view, 200, 60); // 必须设置,否则获得焦点后页面上其他地方点击无响应 menu.setBackgroundDrawable(new BitmapDrawable()); // 设置焦点,必须设置,否则listView无法响应 menu.setFocusable(true); // 设置点击其他地方 popupWindow消失 menu.setOutsideTouchable(true); // 显示在某个位置 menu.showAsDropDown(anchor);
其中R.layout.account_manage_list文件为
<?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"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
其中R.layout.account_manage_long_click_row文件为
<?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:gravity="center_vertical|left"> <TextView android:id="@+id/accountLongClickTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left|center_vertical" android:layout_marginLeft="4dp" style="@style/DefaultFontStyle"> </TextView> </LinearLayout>
注意必须设置PopupWindow的setFocusable(true),否则listView无法响应,
同时需要设置setBackgroundDrawable(new BitmapDrawable());否则setFocusable(true)后,点击页面其他地方便无法响应。
除了setBackgroundDrawable(new BitmapDrawable());还可以使用下面两种解决页面无法响应问题
b、最笨的方法将listView中元素拿出来放到LinearLayout中,对于非listView都无需设置setFocusable(true),从而解决问题,具体可以见http://blog.csdn.net/ihrthk/article/details/7338791
但这种方法对于动态变化的菜单需要配置多份layout文件
2.2 利用GridView实现方格菜单
对于很多应用像uc浏览器的菜单都有更多的选项和分级,实现道理同上面的listView,可以参考http://blog.csdn.net/kkfdsa132/article/details/6403404
同样可能碰到上面的响应问题
参考:
http://developer.android.com/reference/android/widget/PopupWindow.html
关于PopupWindow菜单更炫的效果可参考
http://blog.csdn.net/wanli_smile/article/details/6907433
更多对话框使用可参考
http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html
相关推荐
Android PopupWindow实现右侧、左侧和底部弹出菜单 效果图:http://blog.csdn.net/jianfengwen/article/details/48629217
Android PopupWindow实现弹出菜单.zip
Android PopupWindow实现屏幕底部菜单,效果中包括了圆形的悬浮按钮,这一个圆角按钮实现方法有很多,本例中是使用ImageView来实现。另外效果还包括了弹出的菜单,之前思路有两种,第一种,写布局实现,第二种,弹出...
在使用UC-WebBrowser时,你会发现它...其实,它的本身是PopupWindow或者是AlertDialog对话框,在里面添加两个GridView控件,一个是菜单标题栏,一个是菜单选项。菜单选项视图的切换可以通过适配器的变换,轻松地实现。
安卓Android源码——Android之用PopupWindow实现弹出菜单.zip
Android源码——PopupWindow实现弹出菜单.zip
Android之用PopupWindow实现弹出listview形式菜单.rar
android页面开发中会碰到这种需求:点击页面顶部工具栏某个按钮时需要弹出下拉菜单。该工程利用PopupWindow实现了点击工具栏弹出下拉菜单的功能的主要实现类 .
Android PopupWindow重写系统菜单
android ICS式下拉菜单实现详解 PopupWindow
Android之用PopupWindow实现弹出菜单.rar
android通过使用popupwindow控件实现自定义menu菜单
之用PopupWindow实现弹出菜单.zip
Android之用PopupWindow实现弹出菜单.zip源码资源下载Android之用PopupWindow实现弹出菜单.zip源码资源下载
myandroidmenu_PopupWindow实现Android应用的弹出菜单.rar
Android之用PopupWindow实现弹出菜单.zip
Android用PopupWindow实现的Popup菜单.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
简单封装一个精美、好用的菜单型PopupWindow