美文网首页
Android PopupWindow

Android PopupWindow

作者: GODANDDEVIL | 来源:发表于2020-03-25 17:18 被阅读0次

    PopupWindow与AlertDialog的区别:
    AlertDialog本身默认显示在屏幕最中间(可通过设置WindowManager参数来改变位置)
    PopupWindow可以指定显示位置的

    1、 PopupWindow
    (1)构造方法:

    //方法一:
    public PopupWindow (Context context)
    //方法二:
    public PopupWindow(View contentView)
    //方法三:
    public PopupWindow(View contentView, int width, int height)
    //方法四:
    public PopupWindow(View contentView, int width, int height, boolean focusable)
    

    其中,View contentView,int width,int height这三个是必须设置的,不然PopupWindow无法弹出显示。这里说一下为什么样强制设置contentView,因为PopupWindow没有默认布局,它的布局只有通过我们自己设置才行。

    (2)显示需要调用的方法

    //相对某个控件的位置(正左下方),无偏移
    showAsDropDown(View anchor):
    //相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上
    showAsDropDown(View anchor, int xoff, int yoff):
    //相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
    showAtLocation(View parent, int gravity, int x, int y):
    

    (3)、 可设置的属性与方法

    //隐藏PopupWindow
    public void dismiss()
    //设置可聚焦
    public void setFocusable(boolean focusable)
    //设置可触摸
    public void setTouchable(boolean touchable)
    //设置点击外部区域可以取消popupWindow
    public void setOutsideTouchable(boolean touchable)
    //设置背景
    public void setBackgroundDrawable(Drawable background)
    //设置弹窗弹出的动画高度
    public void setElevation(float elevation)
    //设置PopupWindow叠放效果
    setOverlapAnchor(boolean value);
    
    //PopupWindow监听拦截指定触摸事件
    popupWindow.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
              //如果这里设置返回true,说明你会消耗这个触摸事件,不会向下传递到内容view里
                    return false;
                }
            });
    
    //PopupWindow监听取消事件
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
    
                }
            });
    

    2、PopupWindow的使用

    //获得xml布局View
     View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.xxx, null);
    //使用构造函数实例化
    mPopWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
    //为PopupWindow设置布局
    mPopWindow.setContentView(contentView);
    
    //显示PopupWindow
    View rootview = LayoutInflater.from(MainActivity.this).inflate(R.layout.xxx, null);
    mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
    

    一定要注意的是,PopupWindow中各个控件的所在的布局是contentView的布局,而不是在Activity的布局,所以获取contentView里的控件时,在findViewById(R.id.xxx)前要先获取contentView。

    3、添加动画
    定义动画:

    RotateAnimation rotate = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF,
                    0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    rotate.setDuration(3000);
    
    

    要使动画生效,PopupWindow必须设置一个背景,这里背景设为透明色:

      mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    

    动画实例:

    mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    mPopupWindow.showAsDropDown(btn,100,100);
    RotateAnimation rotate = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF,
                    0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    rotate.setDuration(3000);
    view.startAnimation(rotate);
    

    使用setAnimationStyle方法添加动画
    为PopupWindow添加动画可以调用setAnimationStyle方法,该方法只有一个参数,就是指定动画的样式,因此我们需要定义动画资源和样式资源。
    下面是一个“滑入滑出”动画:

    <!-- res/anim/translate_in.xml -->
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:fromXDelta="0"
            android:toXDelta="0"
            android:fromYDelta="100%"
            android:toYDelta="0"
            android:duration="200" >
        </translate>
    </set>
    
    <!-- res/anim/translate_out.xml -->
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:fromXDelta="0"
            android:toXDelta="0"
            android:fromYDelta="0"
            android:toYDelta="100%"
            android:duration="200" >
        </translate>
    </set>
    

    然后定义“滑动”动画样式:

    <!-- res/values/styles.xml -->
    <style name="animTranslate">
             <item name="android:windowEnterAnimation">@anim/translate_in</item>
             <item name="android:windowExitAnimation">@anim/translate_out</item>
     </style>
    

    添加动画:

    window.setAnimationStyle(R.style.animTranslate);
    

    在弹窗出现后让背景变暗,并在弹窗消失后让背景还原:

    window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp=getWindow().getAttributes();
                lp.alpha=1.0f;
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(lp);
            }
        });
    
        window.showAtLocation(activityPopup, Gravity.BOTTOM, 0, 0);
        WindowManager.LayoutParams lp=getWindow().getAttributes();
        lp.alpha=0.3f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(lp);
    

    摘自:
    https://blog.csdn.net/harvic880925/article/details/49272285#t0
    https://www.cnblogs.com/jzyhywxz/p/7039503.html

    相关文章

      网友评论

          本文标题:Android PopupWindow

          本文链接:https://www.haomeiwen.com/subject/kkkxuhtx.html