android AlertDialog自定义全屏显示

1、定义dialog style如下:

    

2、java代码

        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.DialogStyle);
        final AlertDialog dialog = builder.create();
        dialog.show();  // show之后在dialog的window上添加布局
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = point.x;  // 设置宽度和高度
        lp.height = point.y;
        window.setAttributes(lp);
        window.setContentView(R.layout.dialog_layout);
        Button btn = window.findViewById(R.id.ok_btn);
        if (btn != null) {
            btn.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }

 

你可能感兴趣的:(android)