当前位置: 代码迷 >> Android >> Android PopupWindow引见及实现菜单效果
  详细解决方案

Android PopupWindow引见及实现菜单效果

热度:85   发布时间:2016-04-28 05:30:01.0
Android PopupWindow介绍及实现菜单效果

??

? ? ? ? 转自:http://trinea.iteye.com/blog/1461095

?

? ? ? ? 本文主要介绍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代码??收藏代码
  1. //?list的数据源??
  2. List<HashMap<String,?String>>?list?=?new?ArrayList<HashMap<String,?String>>();??
  3. HashMap<String,?String>?map1?=?new?HashMap<String,?String>();??
  4. map1.put("menuName",?"菜单1");??
  5. HashMap<String,?String>?map2?=?new?HashMap<String,?String>();??
  6. map2.put("menuName",?"菜单2");??
  7. HashMap<String,?String>?map3?=?new?HashMap<String,?String>();??
  8. map3.put("menuName",?"菜单3");??
  9. list.add(map1);??
  10. list.add(map2);??
  11. list.add(map3);??
  12. SimpleAdapter?listAdapter?=?new?SimpleAdapter(AccountManageActivity.this,?list,??
  13. ??????????????????????????????????????????????R.layout.account_manage_long_click_row,??
  14. ??????????????????????????????????????????????new?String[]?{PopupWindowMenu.FIRST_COLUMN},??
  15. ??????????????????????????????????????????????new?int[]?{R.id.accountLongClickTextView});??
  16. ??
  17. View?view?=?getLayoutInflater().inflate(R.layout.account_manage_list,?null);??
  18. ListView?listView?=?(ListView)view.findViewById(R.id.listView);??
  19. listView.setAdapter(listAdapter);??
  20. ??
  21. //?PopupWindow定义,显示view,以及初始化长和宽??
  22. PopupWindow?menu?=?new?PopupWindow(view,?200,?60);??
  23. //?必须设置,否则获得焦点后页面上其他地方点击无响应??
  24. menu.setBackgroundDrawable(new?BitmapDrawable());??
  25. //?设置焦点,必须设置,否则listView无法响应??
  26. menu.setFocusable(true);??
  27. //?设置点击其他地方?popupWindow消失????
  28. menu.setOutsideTouchable(true);??
  29. //?显示在某个位置??
  30. menu.showAsDropDown(anchor);??

?其中R.layout.account_manage_list文件为

Java代码??收藏代码
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <LinearLayout??
  3. ??xmlns:android="http://schemas.android.com/apk/res/android"??
  4. ??android:layout_width="match_parent"??
  5. ??android:layout_height="match_parent">??
  6. ??<ListView???
  7. ????android:id="@+id/listView"??
  8. ????android:layout_width="match_parent"??
  9. ????android:layout_height="wrap_content">??
  10. ????</ListView>??
  11. </LinearLayout>??

? ? ? ?其中R.layout.account_manage_long_click_row文件为

Xml代码??收藏代码
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <LinearLayout??
  3. ????xmlns:android="http://schemas.android.com/apk/res/android"??
  4. ????android:layout_width="match_parent"??
  5. ????android:layout_height="match_parent"??
  6. ????android:gravity="center_vertical|left">??
  7. ????<TextView??
  8. ????????android:id="@+id/accountLongClickTextView"??
  9. ????????android:layout_width="wrap_content"??
  10. ????????android:layout_height="wrap_content"??
  11. ????????android:gravity="left|center_vertical"??
  12. ????????android:layout_marginLeft="4dp"??
  13. ????????style="@style/DefaultFontStyle">??
  14. ????????</TextView>??
  15. </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

  相关解决方案