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

直接上代码 ,核心方法。

  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. 第一个shell程序

    前言:我为什么又来学习shell呢?因为这个轻量级的编程小脚本语言能够帮我处理一些基于linux的复杂手工工作.真是一言难尽,学会一门又来一门!! 看了2天这个教程,试着写了一个小脚本,没啥技术含量, ...

  2. Zend Framework2 入门教程(转)

    转载自: http://my.oschina.net/lai1362000/blog/201301 重申:这本书作者的截图我都放上去了,没侵权啊. 别问那么多,我只是一个安静的搬砖工. 摘要 Zend ...

  3. tpshop全局公共方法

    TPshop 全局公告函数库 前后台可用  这里只列出有哪些方法, 简单描述, 具体的函数体查看 Application\Common\Common\function.php 文件 <?php ...

  4. js json日期格式转换日期方法

      接收json数据,日期格式为:"\/Date(1414078309687)\/" var value = "/Date(1414078309687)/"; ...

  5. python调用ansible接口API执行命令

    python版本:Python 2.6.6 ansible版本:ansible 2.3.1.0      下载地址:https://releases.ansible.com/ansible/ 调用脚本 ...

  6. spring oxm入门(包含demo)

    O/X Mapper 是什么? Spring 3.0 的一个新特性是 O/X Mapper.O/X 映射器这个概念并不新鲜,O 代表 Object,X 代表 XML.它的目的是在 Java 对象(几乎 ...

  7. Quorumpeps 群体感应数据库简介

    群体感应的定义: 细菌能自发产生.释放一些特定的信号分子,并能感知其浓度变化,调节微生物的群体行为, 这一调控系统称为群体感应.细菌群体感应参与包括人类.动植物病原菌致病力在内的多种生物学功能的调节. ...

  8. c++ A类包含B类指针,B类包含A类指针的情况

    #include<stdio.h> class Bclass; class Aclass { public: friend Bclass; void func() { pB->fun ...

  9. nginx配置一、二级域名、多域名对应(api接口、前端网站、后台管理网站)

    前提:安装好nginx,如果已经启动nginx,先停止,命令: ./usr/local/nginx/sbin/nginx -s stop 修改nginx配置 vi /usr/local/nginx/c ...

  10. UIImage 读取图片内存优化

    在图片处理时,我们总会遇到一些内存优化的问题. ​这里介绍的是其中一种内存优化处理方式. 场景: App 运行很卡,然后我用 Instruments 中的相关工具查看对象的内存占用情况,发现当图片加载 ...