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

直接上代码 ,核心方法。

  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. 怎么学数学[How to Study Math]

  2. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.6

    If $\sen{A}<1$, then $I-A$ is invertible, and $$\bex (I-A)^{-1}=I+A+A^2+\cdots, \eex$$ aa converg ...

  3. 基于Ubuntu 15.04 LTS编译Android5.1.0源代码 (转)

    原文链接:http://blog.csdn.net/yuxiangyunei/article/details/45365235   环境: ubuntu:ubuntu-15.04-desktop-am ...

  4. jvm 漫谈 笔记

    1.Jvm到底是什么呢? Jvm其实就是模拟一台计算机,每种cpu都有自己的指令集,jvm自己设置一套指令集,这就是我满看的的字节码,然后jvm需要执行这些字节码,其实这些字节码最终直接对应到cpu的 ...

  5. 计算N的阶层

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

  6. java web工程发布以及解决tomcat闪退

    1.tomcat闪退 a.环境变量错误 startup.bat最后假如PAUSE进入调试状态,双击startup.bat,可以看到错误,根据错误提示设置相应的环境变量,JAVA_HOME等. b.ec ...

  7. GCC编译源代码的四个步骤【转】

    GCC编译C源代码有四个步骤:预处理---->编译---->汇编---->链接. 可以利用GCC的参数来控制执行的过程,这样就可以更深入的了解编译C程序的过程. 下面将通过对一个程序 ...

  8. 【noip2012】疫情控制

    题意: 给出一颗n个节点有边权的树 和m个军队所在的位置 军队从某节点移动到相邻节点要花费边长度的时间 求最少要多少时间使得根节点(编号为1)到每个叶子的路径上最少有一支军队(根节点不能有军队) 题解 ...

  9. Linux shell 常用

    使用Linux shell是我每天的基本工作,但我经常会忘记一些有用的shell命令和l技巧.当然,命令我能记住,但我不敢说能记得如何用它执行某个特定任务.于是,我开始在一个文本文件里记录这些用法,并 ...

  10. POJ2723-Get Luffy Out(2-SAT)

    题意:有m扇门,每个门上有两把锁,打开任意一个锁都可以打开这扇门.门要按顺序一个一个打开. 现在有n对不同的钥匙,每对钥匙只能用其中一个,问最多能打开多少门. 题解:对钥匙建图,门是限制条件来建边.每 ...