zoukankan      html  css  js  c++  java
  • PopupWindow 常用方法学习笔记

    PopupWindow可以实现浮层效果,而且可以自定义显示位置,出现和退出时的动画。

    首先解析一个View

    LayoutInflater inflater = getLayoutInflater();
    final View view = inflater.inflate(R.layout.popup, null);

    创建PopupWindow 构造参数 PopupWindow(View contentView, int width, int height, boolean focusable)。

    contentView为要显示的view,width和height为宽和高,值为像素值,也可以是MATCHT_PARENT和WRAP_CONTENT。

    focusable为是否可以获得焦点,这是一个很重要的参数。

    这里需要为popupWindow设置一个背景,点击其他区域才能让PopupWindow消失

    PopupWindow popupWindow = new PopupWindow(view, 400, 400, true);
    popupWindow.setBackgroundDrawable(new BitmapDrawable());

    显示PopupWindow

    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出现时的动画,popup_enter.xml 

    <?xml version="1.0" encoding="utf-8"?>  
    <set xmlns:android="http://schemas.android.com/apk/res/android">  
        <scale android:fromXScale="1.0" android:toXScale="1.0"  
            android:fromYScale="0" android:toYScale="1.0" 
            android:pivotX="50%" android:pivotY="0%"
            android:duration="100" />  
    </set>

    PopupWindow消失时的动画,popup_exit.xml 

    <?xml version="1.0" encoding="utf-8"?>  
    <set xmlns:android="http://schemas.android.com/apk/res/android">  
        <scale  
            android:fromXScale="1.0" android:toXScale="1.0"  
            android:fromYScale="1.0" android:toYScale="0"  
            android:pivotX="50%" android:pivotY="0%"  
            android:duration="50" />  
    </set>

    再设定动画的style

    <style name="PopupAnimation" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/popup_enter</item>
        <item name="android:windowExitAnimation">@anim/popup_exit</item>
    </style>

    最后通过Java代码设置动画 

    popupWindow.setAnimationStyle(R.style.PopupAnimation);  

    另外在记录一个从屏幕底部弹出的动画

    menu_bottombar_in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
        <translate
            android:duration="250"
            android:fromYDelta="100.0%"
            android:toYDelta="0.0" />
    </set>

    menu_bottombar_out.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
        <translate
            android:duration="250"
            android:fromYDelta="0.0"
            android:toYDelta="100%" />
    </set>

    定义样式

    <style name="anim_menu_bottombar">
        <item name="android:windowEnterAnimation">@anim/menu_bottombar_in</item>
        <item name="android:windowExitAnimation">@anim/menu_bottombar_out</item>
    </style>

    最后显示showAtLocation(findViewById(R.id.layout_main), Gravity.BOTTOM, 0, 0);

  • 相关阅读:
    c++ stl algorithm: std::find, std::find_if
    mysql---多表关联
    使用hadoop命令rcc生成Record 一个简单的方法来实现自己的定义writable对象
    Nexon由Xsolla全球支付服务
    configure.ac:20: error: Autoconf version 2.65 or higher is required
    的无线通信网络的学习LTE的关键技术HARQ(20141217)
    JAVA 公众微信的开放源码项目管理合作伙伴招募的版本号
    【工具】JAVA 在单元读取文件并比较
    linux下如何编译python生成libpython2.5.so动态库
    将主机IDS OSSEC日志文件存入MYSQL的方法
  • 原文地址:https://www.cnblogs.com/daxin/p/3784749.html
Copyright © 2011-2022 走看看