自定义Dialog样式,修改Dialog背景

        final Dialog dialog = new Dialog(this);
        View view = LayoutInflater.from(this).inflate(R.layout.xxx, null);  //自定义样式
        TextView btn = view.findViewById(R.id.finish_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setContentView(view);
        dialog.getWindow().setBackgroundDrawableResource(R.drawable.bg_transparent);//设置默认背景为透明,默认为windowBackground;
        dialog.setCancelable(false);  //禁止点击dialog外界取消
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {  //按键监听
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {  //双击返回按钮退出应用
                    if (System.currentTimeMillis() - FIRST_CLICK_EXIT_TIME <=                    DOUBLE_CLICK_EXIT_INTERVAL_TIME) {
                        System.exit(0);
                    } else {
                        FIRST_CLICK_EXIT_TIME = System.currentTimeMillis();
                    }
                    return true;
                }
                return false;
            }
        });
        dialog.show();

 

你可能感兴趣的:(安卓开发)