zoukankan      html  css  js  c++  java
  • android:PopupWindow的使用场景和注意事项

    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。这个view,你能够使用LayoutInflater.from(context).inflate映射对应的xml文件

    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



  • 相关阅读:
    spring3.0使用annotation完全代替XML(续)
    spring3.0使用annotation完全代替XML
    Hibernate Session中的save(),update(),delete(),saveOrUpdate() 细粒度分析
    SpringMVC介绍之Validation
    Mysql 5.7 使用SSL安全连接
    深入分析Java ClassLoader原理
    Hibernate 动态分页查询
    【翻译自mos文章】Linux的/var/log/messages是空的(0k),messages.0, messages.1也是空的
    <html>
    Android Notification和权限机制探讨
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7225989.html
Copyright © 2011-2022 走看看