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

直接上代码 ,核心方法。

  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()方法:

在控件的上方:

[java] view plaincopy

 
  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);

左边:

[java] view plaincopy

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

右边:

[html] view plaincopy

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

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

  1. popupWindow 在指定位置上的显示

    先看效果图,免得浪费大家时间,看是不是想要的效果 . 直接上代码 ,核心方法. private void showPopupWindow(View parent) { if (popupWindow  ...

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

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

  3. Android捕捉图像后在SurfaceView上变形显示问题的处理

    我们在Android中经常会使用SurfaceView编写自定义的摄像头,可是有的时候会经常会出现图像的变形,我们就会很郁闷的问这到底是为什么呢?其实这个最根本的原因是SurfaceView和PreV ...

  4. Android程序安装后在模拟器上不显示,并且控制台显示The launch will only sync the application package on the device!

    初学安卓,今天写了一个小例子,可是eclipse控制台却提示 No Launcher activity found! The launch will only sync the application ...

  5. Android 在图片的指定位置添加标记

    这些天,项目里加了一个功能效果,场景是: 假如有一个家居图片,图片里,有各样的家居用品: 桌子,毛巾,花瓶等等,需要在指定的商品处添加标记,方便用户直接看到商品,点击该标记,可以进入到商品详情页 .实 ...

  6. Android RecyView 滑动置指定位置

    1,直接回到顶部 recyview.getLinearLayoutManager().scrollToPositionWithOffset(0, 0); 2,慢慢的回到顶部 private void ...

  7. Android学习记录(7)—Intent中显示意图和隐式意图的用法

    Intent(意图)主要是解决Android应用的各项组件之间的通讯. Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的 ...

  8. Android 学习心得 页面跳转,不显示新页面信息

    原因: 1.新页面的Activity中,public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt ...

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

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

随机推荐

  1. NBUT1457 Sona 莫队算法

    由于10^9很大,所以先离散化一下,把给你的这一段数哈希 时间复杂度O(nlogn) 然后就是分块莫队 已知[L,R],由于事先的离散化,可以在O(1)的的时间更新[l+1,r],[l,r+1],[l ...

  2. Ubuntu firefox falsh

    Ubuntu下为Firefox安装Adobe Flash Player   使用环境:     OS:Ubuntu 12.04 LTS     Browser: Firefox 12.0     Ad ...

  3. 计算N的阶层

    int factorial(int n) { int i, result; ; i <= n; i++) result *= i; return result; } int factorial2 ...

  4. 【暑假】[深入动态规划]UVa 10618 Fun Game

    UVa 10618 Fun Game 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035 思路:   一圈人围坐 ...

  5. 【创建本地仓库】【for Centos】CentOS下创建本地repository

    [日期]2014年4月24日 [平台]Centos 6.5 [工具]httpd yum-utils createrepo [步骤] 1)安装httpd. yum install httpd 2)安装y ...

  6. Hash (poj2002-Squares & poj3349-Snowflake Snow Snowflakes)

    //突然发现好弱,好多基础的算法竟然都不会,哈希这种经典的算法,我貌似基本没怎么做过相关的题0.0 POJ2002 题意:给n个点,问有多少组四个点能组成正方形. 题解:枚举两个点,通过公式算出另外两 ...

  7. 射频识别技术漫谈(11)——Mifare系列卡的共性【worldsing笔记】

    Mifare是NXP公司生产的一系列遵守ISO14443A标准的射频卡,包Mifare S50.Mifare S70.Mifare UltraLight.Mifare Pro.Mifare Desfi ...

  8. 【转载】Restful 简介

    转自阮一峰的网络日志  http://www.ruanyifeng.com/blog/2011/09/restful.html 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种&quo ...

  9. Winform- TreeView的使用例子

    自动递归生成树,点击树节点的事件在treeView1_AfterSelect里编写,不多说了,直接上代码,备注在这里一下 public partial class Form1 : Form { pub ...

  10. 如何创建一个有System用户权限的命令行

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何创建一个有System用户权限的命令行.