Android PopupWindow介绍及实现菜单效果
2013-05-21 22:32
471 查看
Android PopupWindow介绍及实现菜单效果
博客分类: Android界面
Android应用
本文主要介绍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显示
Java代码
// 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文件为
Java代码
<?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代码
<?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());还可以使用下面两种解决页面无法响应问题
a、处理响应
有点麻烦,有兴趣可以自己看看http://zhoudan241.iteye.com/blog/1147730
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介绍及实现菜单效果
- Android PopupWindow介绍及实现菜单效果
- 本文给大家介绍在Android中如何实现顶部导航菜单左右滑动效果。
- android中实现透明悬浮的PopupWindow效果
- android中用PopUpWindow自定义Menu菜单 带半透明效果
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- popupwindow 如何实现弹出菜单效果_popupwindow 实现弹出窗口范例
- 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单
- 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Android自定义popupwindow实现ios中UC浏览器中长按图片弹出的PopupWindow效果
- Android PopupWindow实现右侧、左侧和底部弹出菜单
- Android PopupWindow介绍及实现菜单效果
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- 使用PopupWindow + 2个ListView实现仿 美团/淘宝 多级分类菜单效果
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Android27_OptionsMenu、ContextMenu及PopupWindow自定义菜单
- Android 抽屉效果的导航菜单实现
- Android 属性动画实现的扇形菜单效果