先看效果图,免得浪费大家时间,看是不是想要的效果 。

直接上代码 ,核心方法。

  1. private void showPopupWindow(View parent) {
  2. if (popupWindow == null) {
  3. LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  4. view = layoutInflater.inflate(R.layout.group_list, null);
  5. lv_group = (ListView) view.findViewById(R.id.lvGroup);
  6. Collections.reverse(groups);
  7. GroupAdapter groupAdapter = new GroupAdapter(this, groups);
  8. lv_group.setAdapter(groupAdapter);
  9. popupWindow = new PopupWindow(view, 200, 220);
  10. }
  11. popupWindow.setFocusable(true);
  12. popupWindow.setOutsideTouchable(true);
  13. // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
  14. popupWindow.setBackgroundDrawable(new BitmapDrawable());
  15. WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
  16. int xPos = -popupWindow.getWidth() / 2
  17. + getCustomTitle().getCenter().getWidth() / 2;
  18. popupWindow.showAsDropDown(parent, xPos, 4);
  19. lv_group.setOnItemClickListener(new OnItemClickListener() {
  20. @Override
  21. public void onItemClick(AdapterView<?> adapterView, View view,
  22. int position, long id) {
  23. loadNew(((StringItem)(groups.get(position))).getId());
  24. if (popupWindow != null)
  25. popupWindow.dismiss();
  26. }
  27. });
  28. }

这篇是转载的 。

popupWindow 在控件的各个方向上的显示(上、下、左、右),主要用到popupWindow 的showAtLocation()方法:

在控件的上方:

  1. private void showPopUp(View v) {
  2. LinearLayout layout = new LinearLayout(this);
  3. layout.setBackgroundColor(Color.GRAY);
  4. TextView tv = new TextView(this);
  5. tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  6. tv.setText("I'm a pop -----------------------------!");
  7. tv.setTextColor(Color.WHITE);
  8. layout.addView(tv);
  9. popupWindow = new PopupWindow(layout,120,120);
  10. popupWindow.setFocusable(true);
  11. popupWindow.setOutsideTouchable(true);
  12. popupWindow.setBackgroundDrawable(new BitmapDrawable());
  13. int[] location = new int[2];
  14. v.getLocationOnScreen(location);
  15. popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
  16. }

在控件的其他方向上显示只需修改最后一行代码即可,如:

下方:popupWindow.showAsDropDown(v);

左边:

  1. popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);

右边:

  1. popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);
 
 

popupWindow 在指定位置上的显示的更多相关文章

  1. android学习——popupWindow 在指定位置上的显示

    先看效果图,免得浪费大家时间,看是不是想要的效果 . 直接上代码 ,核心方法. [java] view plaincopy private void showPopupWindow(View pare ...

  2. (转载) popupWindow 指定位置上的显示

    popupWindow 指定位置上的显示 标签: androidpopupWindowpopupWindow具体位置放置 2014-07-09 16:23 1114人阅读 评论(0) 收藏 举报  分 ...

  3. 手把手教popupWindow从下往上,以达到流行效果

    效果如图所看到的,点击開始button,popWindow从下往上出来,再点击popWindow外面,popWindow又从上往下消失 能够看出来,上面的popupWindow是半透明的,后面我会细说 ...

  4. [问题解决]Android7.0上PopupWindow的showAsDropDown位置问题

    [问题解决]Android7.0上PopupWindow的showAsDropDown位置问题 /** * Created by diql on 2017/02/16. */ 问题说明 我的popup ...

  5. $.messager.show扩展:指定位置显示

    扩展了个$.messager.showBySite,根据舍得的位置显示$.messager.show.代码如下: /** * 指定位置显示$.messager.show * options $.mes ...

  6. 问题:asp.net 点击button按钮调到页面顶部;结果:asp.net点击一个按钮,使页面跳转到本面页上的指定位置

    asp.net点击一个按钮,使页面跳转到本面页上的指定位置 (2011-04-19 16:46:51) 转载▼ 标签: it   最近在做一个项目. 用到标题所说的功能. 实现方法: 1.在aspx中 ...

  7. postman上传excel,java后台读取excel生成到指定位置进行备份,并且把excel中的数据添加到数据库

    最近要做个前端网页上传excel,数据直接添加到数据库的功能..在此写个读取excel的demo. 首先新建springboot的web项目 导包,读取excel可以用poi也可以用jxl,这里本文用 ...

  8. 解决使用 Eruda 绑定 dom 未在指定位置显示问题

    前言 开发项目中,使用到 Eruda 打印控制台信息显示 文档:https://github.com/liriliri/eruda 安装 Eruda npm install eruda --save ...

  9. shell如何在指定文件的指定位置后面添加内容

    最近工作中遇到一个问题,想在某个文件的指定位置后面添加一个标志位,要求在shell脚本里实现. 问题说明: 想在sys_config.fex文本的某个字符串后面添加一个flag 例如:sys_conf ...

随机推荐

  1. C语言中内存分配问题:

    推荐: C语言中内存分配 Linux size命令和C程序的存储空间布局 本大神感觉,上面的链接的内容,已经很好的说明了: 总结一下: 对于一个可执行文件,在linux下可以使用 size命令列出目标 ...

  2. 使用Unity中的Box Collider组件完成游戏场景中的碰撞检测功能

    一.介绍 目的:通过Unity自带的组件完成游戏场景中的碰撞检测功能. 软件环境:Unity 2017.3.0f3 二.实现过程 1,在面板中点击Add Component按钮 2,添加Box Col ...

  3. Mac 创建证书(以 创建gdb证书 为例 )

    open /Applications/Utilities/Keychain\ Access.app/ 打开 钥匙串访问 继续继续 创建完毕. Now that we have a certificat ...

  4. e606. Determining Which Component or Window Has the Focus

    // null is returned if none of the components in this application has the focus Component compFocusO ...

  5. (实用)Ubuntu 、CentOS更换国内源

    Ubuntu更换apt-get源 通过编辑/etc/apt/sources.list文件,我们能够更换Ubuntu的默认软件更新源.通常是将其换成一些国内比较知名的源.本文主要列举这些内容. 注意,在 ...

  6. ASP.NET EntityFrameworkCore code first 多对多设计

    摘要:参考网址:https://docs.microsoft.com/zh-cn/ef/core/get-started/full-dotnet/new-db场景:使用ASP.NETEntityFra ...

  7. Linux中mkdir和touch命令区别

    一.目的 本文将介绍linux下新建文件或文件夹.删除文件或文件夹命令.         touch能够新建文件,mkdir用来新建文件夹.rm用来删除文件或文件夹.         本文将选取ubu ...

  8. 3.7 su命令 3.8 sudo命令 3.9 限制root远程登录

    3.7 su命令 3.8 sudo命令 3.9 限制root远程登录 su命令 切换用户 [root@centos_1 ~]# su - xiaobo [root@centos_1 ~]# su - ...

  9. ZooKeeper ACL权限设置

    ZK的节点有5种操作权限:CREATE.READ.WRITE.DELETE.ADMIN 也就是 增.删.改.查.管理权限,这5种权限简写为crwda(即:每个单词的首字符缩写)注:这5种权限中,del ...

  10. Python 网络编程相关知识学习

    Python 网络编程 Python 提供了两个级别访问的网络服务.: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的 ...