欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android在弹窗不消失的情况下依然能响应外部事件

程序员文章站 2022-03-26 10:18:09
1,dialog的方式public class OffenDestDialog extends Dialog { private Context context; public OffenDestDialog(@NonNull Context context) { super(context); this.context = context; } @Override protected void onCreate(Bund...

1,dialog的方式

public class OffenDestDialog extends Dialog {

    private Context context;

    public OffenDestDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_offen_dest);
        setCanceledOnTouchOutside(false);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
        initView();
    }

    private void initView() {

    }

    @Override
    public void show() {
        super.show();
        getWindow().setDimAmount(0);//设置背景透明
        getWindow().setDimAmount(0);
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = context.getResources().getDisplayMetrics().widthPixels / 3;
        layoutParams.height = context.getResources().getDisplayMetrics().heightPixels / 4 * 3;
        layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
        layoutParams.x = 100;
        layoutParams.y = 100;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }

2通过WindowManager添加一个自定义布局

 OffenDestDialog offenDestDialog = new OffenDestDialog(getActivity());
        WindowManager wm = getActivity().getWindowManager();
        int width = getActivity().getResources().getDisplayMetrics().widthPixels / 3;
        int heigh = getActivity().getResources().getDisplayMetrics().heightPixels / 4 * 3;
        WindowManager.LayoutParams param= new WindowManager.LayoutParams(width,heigh);
        param.width = width;
        param.height = heigh;
        param.gravity = Gravity.TOP | Gravity.LEFT;
        param.y = Utils.dp2px(100);
        param.x = Utils.dp2px(100);
        param.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        param.format = PixelFormat.RGBA_8888;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            param.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            param.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        wm.addView(offenDestDialog,param);

 

本文地址:https://blog.csdn.net/AmazonUnicon/article/details/110538883