android:PopupWindow的使用场景和注意事项
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android:PopupWindow的使用场景和注意事项,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1965字,纯文字阅读大概需要3分钟。
内容图文
1.PopupWindow的特点
借用Google官方的说法:
“A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.”
也就是说,popupwindow是activity上方的一个悬浮容器,它可以显示任意的视图View,很霸气的样子。下面看一下,它如何使用的。
2.初始化PopupWindow的一些特性
举例:
PopupWindow popupWindow = new PopupWindow(getApplicationContext()); popupWindow.setContentView(contentView);//可以设置任意的View popupWindow.setWidth(LayoutParams.WRAP_CONTENT);//设置宽度 popupWindow.setHeight(LayoutParams.WRAP_CONTENT);//高度 popupWindow.setAnimationStyle(R.anim.abc_fade_in);//显示的动画 popupWindow.setFocusable(true);//设置是否获取焦点
其中,contentView是你想要显示的View。
3.PopupWindow的显示和隐藏
显示的方法:
public void showAtLocation (View parent, int gravity, int x, int y) Added in API level 1 Display the content view in a popup window at the specified location. If the popup window cannot fit on screen, it will be clipped. See WindowManager.LayoutParams for more information on how gravity and the x and y parameters are related. Specifying a gravity of NO_GRAVITY is similar to specifying Gravity.LEFT | Gravity.TOP. Parameters parent a parent view to get the getWindowToken() token from gravity the gravity which controls the placement of the popup window x the popup‘s x location offset y the popup‘s y location offset
popupWindow.showAtLocation(contentView,Gravity.CENTER,0,0);//设置居中
popupWindow.showAtLocation(contentView,Gravity.NO_GRAVITY,x,y);//显示窗口的以(x,y)为左上角的位置
隐藏:
if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); popupWindow = null; }
相关:注意,在计算view的位置时:
Android里面提供了一些方法可以获取View在屏幕中的位置。
1).getLocationOnScreen ,计算该视图在全局坐标系中的x,y值,获取在当前屏幕内的绝对坐标(该值从屏幕顶端算起,包括了通知栏高度)。
2).getLocationInWindow ,计算该视图在它所在的widnow的坐标x,y值。
3)getLeft , getTop, getBottom, getRight, 这一组是获取相对在它父亲布局里的坐标。
相关:popupwindow动画:http://blog.csdn.net/wl455624651/article/details/7798879
更多交流,Android开发联盟QQ群:272209595
原文:http://blog.csdn.net/nupt123456789/article/details/40152245
内容总结
以上是互联网集市为您收集整理的android:PopupWindow的使用场景和注意事项全部内容,希望文章能够帮你解决android:PopupWindow的使用场景和注意事项所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。