Android Popwindow的使用(从屏幕下方往上弹起,背景变暗)

根据android的开发文档对PopupWindow的介绍:
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.
即一种能够显示任意视图的容器,它是悬浮在当前activity上面的;它跟dialog非常类似;
PopupWindow直译过来就是弹窗,现在很多应用都会用到它,比如输入法弹起。

PopupWindow的构造器非常多种,我们常用这种:PopupWindow(View contentView, int width, int height)即指定相应的view跟宽高,这样已经基本满足一般的需求了。
一般的使用步骤如下:
1.获取xml资源中的view:View popupWindow_view = getLayoutInflater().inflate(R.layout.popwindow, null, false);
2.实例化PopupWindow:popupWindow = new PopupWindow(View contentView, int width, int height)
3.设置显示的位置:popupWindow.showAtLocation(View parent, int gravity, int x, int y)

当然还有很多属性可以设置,网上好多种解释,但我尝试设置那些属性,不同手机好像有不同效果,可能是版本不兼容的问题,需要使用的时候自己慢慢尝试摸索吧···

下面是简单的demo:

 private PopupWindow popupWindow;
 private void showPopupWindow() {
        backgroundAlpha(0.5f);//让背景变暗
        View popupWindow_view = getLayoutInflater().inflate(R.layout.popwindow, null, false);

        popupWindow = new PopupWindow(popupWindow_view,   ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setAnimationStyle(R.style.popWindowAnimation);//设置弹出和消失的动画
        popupWindow.setFocusable(true);// 取得焦点
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.showAtLocation(popupWindow_view, Gravity.BOTTOM, 0, 0);
        // 点击窗体内其他地方消失
        popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (popupWindow != null && popupWindow.isShowing()) {
                    backgroundAlpha(1);
                    popupWindow.dismiss();
                    popupWindow = null;
                }
                return false;
            }
        });
        
        //设置消失的监听
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1);
            }
        });
    }

    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }
    
    下面是设置动画:
    在anim文件夹下新建popwindow_pop_anim.xml和popwindow_fade_anim.xml代码如下
    popwindow_fade_anim.xml
        <set xmlns:android="http://schemas.android.com/apk/res/android">
                <translate
                 android:duration="300"
                 android:fromYDelta="0"
                 android:toYDelta="100%" />
                  <alpha
                   android:duration="300"
                   android:fromAlpha="1.0"
                    ndroid:toAlpha="0.0" />
        </set>
        
     popwindow_pop_anim.xml
        <set xmlns:android="http://schemas.android.com/apk/res/android">
                <translate
                android:duration="300"
                android:fromYDelta="100%"
                android:toYDelta="0" />
                <alpha
                android:duration="300"
                android:fromAlpha="0.0"
                android:toAlpha="1.0" />
        </set>
    然后在style中设置
    <style name="popWindowAnimation">
        <item name="android:windowEnterAnimation">@anim/popwindow_pop_anim</item>
        <item name="android:windowExitAnimation">@anim/popwindow_fade_anim</item>
    </style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值