码迷,mamicode.com
首页 > Windows程序 > 详细

ListPopupWindow 列表弹窗 常见弹窗区别

时间:2017-12-28 00:02:40      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:listen   round   gravity   lis   border   apt   tar   href   pdo   

案例

技术分享图片
private void showPopupWindow(final Context context, @NonNull View anchorView) {
    final String[] popWindowItems = {"使用教程", "声音广场"};//显示的内容
    final ListPopupWindow popupWindow = new ListPopupWindow(context);
    popupWindow.setAdapter(adapter);//可以使用通用的BaseAdapter,也可以用PopupArrayAdapter(context, array)这些具有默认UI效果的adapter
    popupWindow.setWidth(ViewUtils.dipToPx(this, 120));//也可以设置具体的值。容易出错的地方
    popupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
    popupWindow.setAnchorView(anchorView);//设置参照控件
    popupWindow.setHorizontalOffset(ViewUtils.dipToPx(context, -16));//相对锚点偏移值,正值表示向右偏移
    popupWindow.setVerticalOffset(ViewUtils.dipToPx(context, -16));//相对锚点偏移值,正值表示向下偏移
    popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.popup_window_selector));
    popupWindow.setDropDownGravity(Gravity.END);//设置对齐方式。Gravity.START表示与参照控件左侧对齐,Gravity.END表示与参照控件右侧对齐。容易出错的地方
    popupWindow.setModal(true);//模态框,设置为true响应物理键
    popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            popupWindow.dismiss();
        }
    });
    popupWindow.show();
}
20
20
 
1
private void showPopupWindow(final Context context, @NonNull View anchorView) {
2
    final String[] popWindowItems = {"使用教程", "声音广场"};//显示的内容
3
    final ListPopupWindow popupWindow = new ListPopupWindow(context);
4
    popupWindow.setAdapter(adapter);//可以使用通用的BaseAdapter,也可以用PopupArrayAdapter(context, array)这些具有默认UI效果的adapter
5
    popupWindow.setWidth(ViewUtils.dipToPx(this, 120));//也可以设置具体的值。容易出错的地方
6
    popupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
7
    popupWindow.setAnchorView(anchorView);//设置参照控件
8
    popupWindow.setHorizontalOffset(ViewUtils.dipToPx(context, -16));//相对锚点偏移值,正值表示向右偏移
9
    popupWindow.setVerticalOffset(ViewUtils.dipToPx(context, -16));//相对锚点偏移值,正值表示向下偏移
10
    popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.popup_window_selector));
11
    popupWindow.setDropDownGravity(Gravity.END);//设置对齐方式。Gravity.START表示与参照控件左侧对齐,Gravity.END表示与参照控件右侧对齐。容易出错的地方
12
    popupWindow.setModal(true);//模态框,设置为true响应物理键
13
    popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
14
        @Override
15
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
16
            popupWindow.dismiss();
17
        }
18
    });
19
    popupWindow.show();
20
}

ListPopupWindow 简介

ListPopupWindow也是在参照控件下方显示列表窗口,不同的是,它在展示上更加灵活,开发者可以自定义列表弹窗的大小与样式。 
下面是ListPopupWindow的常用方法说明: 
  • setAdapter : 设置下拉列表的数据适配器。 
  • setModal : 设置显示模式,设置为true响应物理键
  • setWidth : 设置下拉列表窗口的宽度。 
  • setHeight : 设置下拉列表窗口的高度。 
  • setAnchorView : 设置下拉列表的参照控件,下拉列表在显示时将展现在参照控件的下方。注意:如果不设置参照控件就直接调用show函数,系统不知道要把下拉列表在何处展示,只能是异常退出了。 
  • setDropDownGravity : 设置下拉列表的对齐方式。Gravity.START表示与参照控件左侧对齐,Gravity.END表示与参照控件右侧对齐。注意:该函数只在4.4.2及以上版本中使用。 
  • setOnItemClickListener : 设置列表项的点击监听器。 
  • setHorizontalOffset : 相对锚点偏移值,正值表示向右偏移
  • setVerticalOffset : 相对锚点偏移值,正值表示向下偏移
  • show : 显示下拉列表窗口。 
  • dismiss : 关闭下拉列表窗口。 
  • setOnDismissListener : 设置下拉列表的关闭监听器。

PopupMenu 简介

构造函数 :
public PopupMenu(Context context, View anchor) {
   this(context, anchor, Gravity.NO_GRAVITY);
}
public PopupMenu(Context context, View anchor, int gravity) {
   this(context, anchor, gravity, R.attr.popupMenuStyle, 0);
}
public PopupMenu(Context context, View anchor, int gravity, int popupStyleAttr, int popupStyleRes) { }
7
7
 
1
public PopupMenu(Context context, View anchor) {
2
   this(context, anchor, Gravity.NO_GRAVITY);
3
}
4
public PopupMenu(Context context, View anchor, int gravity) {
5
   this(context, anchor, gravity, R.attr.popupMenuStyle, 0);
6
}
7
public PopupMenu(Context context, View anchor, int gravity, int popupStyleAttr, int popupStyleRes) { }
 构造一个PopupMenu对象,并指定该对象的参照控件,对齐方式。

常用方法:
  • inflate : 根据指定的菜单资源文件 R.menu.***,把具体的菜单项目填充到PopupMenu对象中。
Inflate a menu resource into this PopupMenu. This is equivalent to calling popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu()).
1
 
1
Inflate a menu resource into this PopupMenu. This is equivalent to calling popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu()).
  • show : 显示弹出菜单。
  • dismiss : 关闭弹出菜单。

  • setOnMenuItemClickListener : 设置菜单项的点击监听器。该监听器由接口OnMenuItemClickListener派生而来,要重写onMenuItemClick方法来实现菜单项点击事件。
  • setOnDismissListener : 设置弹出菜单的关闭监听器。
  • setGravity / getGravity : 对齐方式
Sets the gravity used to align the popup window to its anchor view.
If the popup is showing, calling this method will take effect only the next time the popup is shown.
2
 
1
Sets the gravity used to align the popup window to its anchor view.
2
If the popup is showing, calling this method will take effect only the next time the popup is shown.

  • getMenuInflater
  • getMenu()
Returns the Menu associated with this popup. Populate the returned Menu with items before calling show().
1
 
1
Returns the Menu associated with this popup. Populate the returned Menu with items before calling show().
  • getMenuListView
Returns the ListView representing the list of menu items in the currently showing menu.
1
 
1
Returns the ListView representing the list of menu items in the currently showing menu.

常见弹窗的区别

PopupMenu是种显示位置不固定的弹出菜单,其展示位置随着参照控件的位置变化而变化。
下面几种菜单的显示位置都是固定的
  • 选项菜单Options固定显示在屏幕下方
  • 上下文菜单ContextMenu固定显示在屏幕中央
  • 溢出菜单OverflowMenu固定显示在屏幕右上角

PopupMenu、ListPopupWindow和Spinner的区别:
  • 1、查看源码,会发现PopupMenu和Spinner内部都是使用ListPopupWindow实现下拉列表效果,所以ListPopupWindow是基础
  • 2、PopMenu的列表页面无法定制UI,只能显示光秃秃的文字;而ListPopupWindow和Spinner可以通过适配器来设置每项的布局风格,当然ListPopupWindow是最灵活的,不但可在左侧显示列表,还能在右侧显示列表。
  • 3、PopMenu可通过子菜单实现多级菜单效果,而ListPopupWindow和Spinner只有一级列表。
  • 4、ListPopupWindow和Spinner可以设置默认选中项,而PopMenu没有默认选中项。
  • 5、Spinner既可以下拉列表来展示,也可以对话框来展示;而PopupMenu和ListPopupWindow只能以下拉列表展示。
2017-2-20

ListPopupWindow 列表弹窗 常见弹窗区别

标签:listen   round   gravity   lis   border   apt   tar   href   pdo   

原文地址:https://www.cnblogs.com/baiqiantao/p/8127888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!