• 【Android


      创建一个类继承自PopupWindow,编写自定义的PopupWindow类。示例代码如下:

    import android.app.Activity;
    import android.graphics.Color;
    import android.graphics.drawable.ColorDrawable;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.PopupWindow;
    
    /**
     * 自定义的PopupWindow
     */
    public class MyPopWindow extends PopupWindow {
    
        public MyPopWindow(Activity context) {
            // 通过layout的id找到布局View
            View contentView = LayoutInflater.from(context).inflate(R.layout.pop_custom, null);
            // 获取PopupWindow的宽高
            int h = context.getWindowManager().getDefaultDisplay().getHeight();
            int w = context.getWindowManager().getDefaultDisplay().getWidth();
            // 设置PopupWindow的View
            this.setContentView(contentView);
            // 设置PopupWindow弹出窗体的宽高
            this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            // 设置PopupWindow弹出窗体可点击(下面两行代码必须同时出现)
            this.setFocusable(true);
            this.setOutsideTouchable(true); // 当点击外围的时候隐藏PopupWindow
            // 刷新状态
            this.update();
            // 设置PopupWindow的背景颜色为半透明的黑色
            ColorDrawable dw = new ColorDrawable(Color.parseColor("#66000000"));
            this.setBackgroundDrawable(dw);
            // 设置PopupWindow弹出窗体动画效果
            this.setAnimationStyle(R.style.PopWindowAnimStyle);
    
            // 这里也可以从contentView中获取到控件,并为它们绑定控件
        }
    
        // 显示PopupWindow,有两种方法:showAsDropDown、showAtLocation
        public void showPopupWindow(View parent) {
            if (!this.isShowing()) {
                // showAsDropDown方法,在parent下方的(x,y)位置显示,x、y是第二和第三个参数
                // this.showAsDropDown(parent, parent.getWidth() / 2 - 400, 18);
    
                // showAtLocation方法,在parent的某个位置参数,具体哪个位置由后三个参数决定
                this.showAtLocation(parent, Gravity.CENTER, 0, 0);
            } else {
                this.dismiss();
            }
        }
    }

      调用代码:

    MyPopWindow popWindow = new MyPopWindow(MainActivity.this);
    popWindow.showPopupWindow(new View(MainActivity.this));
  • 相关阅读:
    python 加入excel 失败的原因
    Python 比利的滑动验证
    HTML列表
    HTML表格
    HTML图像
    牛客网212D禁书目录Index-题解
    关于RMQ的一些拓展
    LOJ535「LibreOJ Round #6」花火-题解
    [SDOI2011]导弹拦截-题解
    [HNOI2015]开店-题解
  • 原文地址:https://www.cnblogs.com/itgungnir/p/6211292.html
Copyright © 2020-2023  润新知