zoukankan      html  css  js  c++  java
  • poppupwindow android

    Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:

    • AlertDialog的位置固定,而PopupWindow的位置可以随意
    • AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的

    PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下

    • showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
    • showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
    • showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏

    PopupWindow可以实现浮层效果,而且可以自定义显示位置,出现和退出时的动画,比如新浪微博顶部栏的微博分组就是用PopupWindow实现的。

            一、实例化PopupWindow,这里用R.layout.group_list填充mPopupWindow,并指定宽高。

    Java代码  收藏代码
    1. mPopupLayout = getLayoutInflater().inflate(R.layout.group_list, null);  
    2. mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.transparent)); //必须为PopupWindow设置一个背景,点击其他区域才能让PopupWindow消失  
    3. mPopupWindow = new PopupWindow(mPopupLayout, width, height, true);  


            二、指定PopupWindow的显示位置

    Java代码  收藏代码
    1. //mAncorView 是页面中的某个View,默认是将mPopupWindow与mAncorView的左下角对齐,如果空间不够显示,则将将mPopupWindow与 mAncorView的左上角对齐。offsetX和offsetY是mPopupWindow位置的相对偏移,很容易理解。  
    2.         mPopupWindow.showAsDropDown(mAncorView, offsetX, offsetY);  



    也可使用下面方法

    Java代码  收藏代码
    1. //下面的方法是用屏幕上的绝对坐标显示,mPopupWindow  
    2. //我们往往不知道mPopupWindow要显示的精确位置,通常先计算页面上某个元素mView的位置,在进行偏移  
    3.           
    4. //得到mView在屏幕中的坐标  
    5. int [] pos = new int[2];  
    6. mView.getLocationOnScreen(pos);  
    7. offsetY = pos[1] + mView.getHeight();  
    8. offsetX = 0;  
    9.           
    10. //显示mPopupWindow  
    11. mPopupWindow.showAtLocation(mView, Gravity.TOP|Gravity.CENTER_HORIZONTAL, offsetX, offsetY);//这里的第一个参数没搞明白什么用,android官方文档说是为了获取token  



            三、为PopupWindow指定动画
    先定义动画
    PopupWindow出现时的动画,popup_enter.xml

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>    
    2. <set xmlns:android="http://schemas.android.com/apk/res/android">    
    3.     <scale android:fromXScale="1.0" android:toXScale="1.0"    
    4.         android:fromYScale="0" android:toYScale="1.0"   
    5.         android:pivotX="50%" android:pivotY="0%"  
    6.         android:duration="100" />    
    7. </set>  



    PopupWindow消失时的动画,popup_exit.xml

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>    
    2. <set xmlns:android="http://schemas.android.com/apk/res/android">    
    3.     <scale    
    4.         android:fromXScale="1.0" android:toXScale="1.0"    
    5.         android:fromYScale="1.0" android:toYScale="0"    
    6.         android:pivotX="50%" android:pivotY="0%"    
    7.         android:duration="50" />    
    8. </set>  



    再设定动画的style

    Xml代码  收藏代码
    1. <style name="PopupAnimation" parent="android:Animation">  
    2.     <item name="android:windowEnterAnimation">@anim/popup_enter</item>  
    3.     <item name="android:windowExitAnimation">@anim/popup_exit</item>  
    4. </style>  



    最后通过Java代码设置动画

    Java代码  收藏代码
     
      1. mPopupWindow.setAnimationStyle(R.style.PopupAnimation); 

        参考:http://ipjmc.iteye.com/blog/1455049

    http://blog.csdn.net/tianjf0514/article/details/7570302#

    http://blog.sina.com.cn/s/blog_5da93c8f0100ygih.html

    http://blog.csdn.net/wwj_748/article/details/25653409

    http://www.jb51.net/article/32031.htm

    http://test-angel.iteye.com/blog/1627208

    弹出方式:http://www.jb51.net/article/32031.htm

  • 相关阅读:
    扯蛋的密码规则
    【转】mysql安全基线设置
    阿里云安全基线 记录如下 不定时更新
    解决Apache的错误日志巨大的问题以及关闭Apache web日志记录
    cms如何绑定二级域名
    宝塔面板定时/同步备份网站及数据库至FTP存储空间完整教程
    宝塔部署项目报Warning: require(): open_basedir restriction in effect的解决方案
    git学习笔记(一)—— git环境搭建
    vim学习笔记(一)—— vim安装方法
    Intel Edison学习笔记(二)—— 入门环境配置
  • 原文地址:https://www.cnblogs.com/yc3120/p/3880379.html
Copyright © 2011-2022 走看看