android学习——popupWindow 在指定位置上的显示
先看效果图,免得浪费大家时间,看是不是想要的效果 。
直接上代码 ,核心方法。
- private void showPopupWindow(View parent) {
- if (popupWindow == null) {
- LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- view = layoutInflater.inflate(R.layout.group_list, null);
- lv_group = (ListView) view.findViewById(R.id.lvGroup);
- Collections.reverse(groups);
- GroupAdapter groupAdapter = new GroupAdapter(this, groups);
- lv_group.setAdapter(groupAdapter);
- popupWindow = new PopupWindow(view, 200, 220);
- }
- popupWindow.setFocusable(true);
- popupWindow.setOutsideTouchable(true);
- // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
- popupWindow.setBackgroundDrawable(new BitmapDrawable());
- WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
- int xPos = -popupWindow.getWidth() / 2
- + getCustomTitle().getCenter().getWidth() / 2;
- popupWindow.showAsDropDown(parent, xPos, 4);
- lv_group.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> adapterView, View view,
- int position, long id) {
- loadNew(((StringItem)(groups.get(position))).getId());
- if (popupWindow != null)
- popupWindow.dismiss();
- }
- });
- }
这篇是转载的 。
popupWindow 在控件的各个方向上的显示(上、下、左、右),主要用到popupWindow 的showAtLocation()方法:
在控件的上方:
- private void showPopUp(View v) {
- LinearLayout layout = new LinearLayout(this);
- layout.setBackgroundColor(Color.GRAY);
- TextView tv = new TextView(this);
- tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- tv.setText("I'm a pop -----------------------------!");
- tv.setTextColor(Color.WHITE);
- layout.addView(tv);
- popupWindow = new PopupWindow(layout,120,120);
- popupWindow.setFocusable(true);
- popupWindow.setOutsideTouchable(true);
- popupWindow.setBackgroundDrawable(new BitmapDrawable());
- int[] location = new int[2];
- v.getLocationOnScreen(location);
- popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
- }
在控件的其他方向上显示只需修改最后一行代码即可,如:
下方:popupWindow.showAsDropDown(v);
左边:
- popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);
右边:
- popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);
android学习——popupWindow 在指定位置上的显示的更多相关文章
- popupWindow 在指定位置上的显示
先看效果图,免得浪费大家时间,看是不是想要的效果 . 直接上代码 ,核心方法. private void showPopupWindow(View parent) { if (popupWindow ...
- (转载) popupWindow 指定位置上的显示
popupWindow 指定位置上的显示 标签: androidpopupWindowpopupWindow具体位置放置 2014-07-09 16:23 1114人阅读 评论(0) 收藏 举报 分 ...
- Android捕捉图像后在SurfaceView上变形显示问题的处理
我们在Android中经常会使用SurfaceView编写自定义的摄像头,可是有的时候会经常会出现图像的变形,我们就会很郁闷的问这到底是为什么呢?其实这个最根本的原因是SurfaceView和PreV ...
- Android程序安装后在模拟器上不显示,并且控制台显示The launch will only sync the application package on the device!
初学安卓,今天写了一个小例子,可是eclipse控制台却提示 No Launcher activity found! The launch will only sync the application ...
- Android 在图片的指定位置添加标记
这些天,项目里加了一个功能效果,场景是: 假如有一个家居图片,图片里,有各样的家居用品: 桌子,毛巾,花瓶等等,需要在指定的商品处添加标记,方便用户直接看到商品,点击该标记,可以进入到商品详情页 .实 ...
- Android RecyView 滑动置指定位置
1,直接回到顶部 recyview.getLinearLayoutManager().scrollToPositionWithOffset(0, 0); 2,慢慢的回到顶部 private void ...
- Android学习记录(7)—Intent中显示意图和隐式意图的用法
Intent(意图)主要是解决Android应用的各项组件之间的通讯. Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的 ...
- Android 学习心得 页面跳转,不显示新页面信息
原因: 1.新页面的Activity中,public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt ...
- 手把手教popupWindow从下往上,以达到流行效果
效果如图所看到的,点击開始button,popWindow从下往上出来,再点击popWindow外面,popWindow又从上往下消失 能够看出来,上面的popupWindow是半透明的,后面我会细说 ...
随机推荐
- 怎么学数学[How to Study Math]
- [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 ...
- 基于Ubuntu 15.04 LTS编译Android5.1.0源代码 (转)
原文链接:http://blog.csdn.net/yuxiangyunei/article/details/45365235 环境: ubuntu:ubuntu-15.04-desktop-am ...
- jvm 漫谈 笔记
1.Jvm到底是什么呢? Jvm其实就是模拟一台计算机,每种cpu都有自己的指令集,jvm自己设置一套指令集,这就是我满看的的字节码,然后jvm需要执行这些字节码,其实这些字节码最终直接对应到cpu的 ...
- 计算N的阶层
int factorial(int n) { int i, result; ; i <= n; i++) result *= i; return result; } int factorial2 ...
- java web工程发布以及解决tomcat闪退
1.tomcat闪退 a.环境变量错误 startup.bat最后假如PAUSE进入调试状态,双击startup.bat,可以看到错误,根据错误提示设置相应的环境变量,JAVA_HOME等. b.ec ...
- GCC编译源代码的四个步骤【转】
GCC编译C源代码有四个步骤:预处理---->编译---->汇编---->链接. 可以利用GCC的参数来控制执行的过程,这样就可以更深入的了解编译C程序的过程. 下面将通过对一个程序 ...
- 【noip2012】疫情控制
题意: 给出一颗n个节点有边权的树 和m个军队所在的位置 军队从某节点移动到相邻节点要花费边长度的时间 求最少要多少时间使得根节点(编号为1)到每个叶子的路径上最少有一支军队(根节点不能有军队) 题解 ...
- Linux shell 常用
使用Linux shell是我每天的基本工作,但我经常会忘记一些有用的shell命令和l技巧.当然,命令我能记住,但我不敢说能记得如何用它执行某个特定任务.于是,我开始在一个文本文件里记录这些用法,并 ...
- POJ2723-Get Luffy Out(2-SAT)
题意:有m扇门,每个门上有两把锁,打开任意一个锁都可以打开这扇门.门要按顺序一个一个打开. 现在有n对不同的钥匙,每对钥匙只能用其中一个,问最多能打开多少门. 题解:对钥匙建图,门是限制条件来建边.每 ...