Unable to add window -- token null is not for an application

参考:http://liuzhichao.com/p/545.html

在自定义Diaglog进行展示时,提示:Unable to add window -- token null is not for an application

这是由于Context的定义不一样造成的,这里不能使用Application的Context,而要使用Activity,因为只有Activity才能展示对话框。

 

修改自定义对话框的大小

TextView view = new TextView(this);
view.setBackgroundColor(Color.RED);
view.setMinWidth(500);
view.setMinHeight(500);
view.setText("HELLO");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog dialog = builder.setView(view).create();
dialog.show();

AlertDialog对话框默认就是居中,而且dialog大小根据setView(view)的view决定的,

*************************************************************

  Window w = mDownloadDialog.getWindow();
  WindowManager.LayoutParams wl = w.getAttributes();
  wl.x = 0;
  wl.y = 52;
  wl.height=300;
  wl.width=300;
  w.setAttributes(wl);

这是改界面的

posted on 2013-06-19 18:21  马来亚  阅读(369)  评论(0编辑  收藏  举报

导航