• Android Popwindow使用总结


    Android Popwindow使用总结

    1.基本使用方法
    View view = getLayoutInflater().inflate(R.layout.activity_photo_preview, null);
    ......
    
     if (popupBigPhoto == null) {
                popupBigPhoto = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
                popupBigPhoto.setOutsideTouchable(true);
                popupBigPhoto.setOnDismissListener(this);
            }
            if (popupBigPhoto.isShowing()) {
                popupBigPhoto.dismiss();
            } else {
                popupBigPhoto.showAtLocation(headview, Gravity.TOP, 0, 0);
            }
    
    
    2.属性方法

    1.基本属性方法

    // 设置PopupWindow的背景
     window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    // 设置PopupWindow是否能响应外部点击事件
     window.setOutsideTouchable(true);
    // 设置PopupWindow是否能响应点击事件
    window.setTouchable(true);
    

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

    window.setOnDismissListener(new PopupWindow.OnDismissListener(){
    @Override
    public void onDismiss() {
         WindowManager.LayoutParams lp=getWindow().getAttributes();
         lp.alpha=1.0f;
         getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHND);
         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);
    

    3.添加动画
    自定义一个动画

    <!-- 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);
    
    3.位置设置
    1. 相对于父布局的位置
    public void showAtLocation(View parent, int gravity, int x, int y)
    
       第二个参数gravity指的是popupWindow在父布局中出现的大致位置。常见的有 Gravity.NO_GRAVITY,Gravity.LEFT,Gravity.RIGHT,Gravity.TOP,Gravity.BOTTOM。
       第三个参数int x指的是以第二个参数gravity指点的位置为原点,popupWindow相对于原点X轴上的位置。x为正popupWindow向右移动,x为负popupWindow向左移动。
       第四个参数int y同X差不多,指的是y轴上的位置。y为正popupWindow向上,y为负popupWindow向下。
    
    1. 相对于某个控件的位置
    public void showAsDropDown(View anchor)
    public void showAsDropDown(View anchor, int xoff, int yoff)
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)
        前两个方法不指定gravity 则popupWindow出现在anchor的正下方。
        第一个参数anchor指的是你的popupWindow相对于的这个控件。
        第二个参数xoff指的是popupWindow相对于原点X轴上的位置。x为正popupWindow向右移动,x为负popupWindow向左移动。
        第三个参数yoff指的是popupWindow相对于原点y轴上的位置。y为正popupWindow向下,y为负popupWindow向上。
    
    4.popwindow被软键盘遮挡实现方式
    private void showPop(View view) {
            if (popWindow != null && imms != null) {
                imms.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
            }
            if (popWindow == null) {
                imms = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                View layout = LayoutInflater.from(this).inflate(R.layout.live_qa_saysth, null);
                  ......
                popWindow = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT, true);
                popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                imms.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
                popWindow.setBackgroundDrawable(new ColorDrawable(0xb0000000));
                popWindow.setOutsideTouchable(true);
            } 
            if (!popWindow.isShowing()) {
                popWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
            } else {
                popWindow.dismiss();
            }
        }
    
    注意点
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    window.setOutsideTouchable(true);
    

    只有同时设置PopupWindow的背景和可以响应外部点击事件,它才能“真正”响应外部点击事件。也就是说,当你点击PopupWindow的外部或者按下“Back”键时,PopupWindow才会消失。

    特殊情况处理:

    1.在popwindow中嵌套viewpager时候,关于定位问题:首先保证viewpager类是同一个,就是没有新new一个类。然后在show的时候记得setCurrentItem()一下就好了。

    参考资料

    Android PopupWindow使用方法小结
    Android中文API——PopupWindow

  • 相关阅读:
    用JS实现汉字转拼音
    jQuery Validate验证框架详解
    移动前端自适应适配布局解决方案和比较
    js获取当前日期时间“yyyy-MM-dd HH:MM:SS”
    jQuery cookie
    dataTable 从服务器获取数据源的两种表现形式
    dataTable 参数说明
    如何在HTML网页中显示HTML标签内容?
    java中构造函数前用public修饰与没有任何修饰符相比,有什么区别?
    面向对象设计
  • 原文地址:https://www.cnblogs.com/it-tsz/p/11182947.html
Copyright © 2020-2023  润新知