zoukankan      html  css  js  c++  java
  • Android PopupWindow介绍及实现菜单效果

    本文主要介绍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());还可以使用下面两种解决页面无法响应问题

    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

    更多对话框使用可参考

    http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html



    已有 0 人发表留言,猛击->>这里<<-参与讨论


    ITeye推荐



  • 相关阅读:
    Java 中常用缓存Cache机制的实现
    Spring普通类获取bean
    系统升级shell
    shell 操作文本内容(修改增加)
    接口实践;接口与抽象类
    Java中@Override的作用
    类实现多个接口的例子
    java 反射实践
    对象主要属性及识别
    java抽象类实践
  • 原文地址:https://www.cnblogs.com/trinea/p/2420101.html
Copyright © 2011-2022 走看看