单击屏幕时隐藏透明对话框视图

时间:2014-06-14 02:25:47

标签: android

我在创建时创建了这个透明窗口,如何才能创建它,而不必按下后退按钮,在按下屏幕上的任何位置时隐藏视图?

代码:

    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.help);
    dialog.setCanceledOnTouchOutside(true);
    final Window window = dialog.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,                        
    WindowManager.LayoutParams.MATCH_PARENT);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.show();

解决方案:

RelativeLayout rl = (RelativeLayout) dialog.findViewById(R.id.relativeLayout);
        rl.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }   
        });

1 个答案:

答案 0 :(得分:0)

使用

dialog.setCancellable(true);

编辑:

@Val您可以在对话框的视图中添加单击侦听器并将其关闭