自定义Dialog_位置 坐标

来源:互联网 发布:计算机算法表示 编辑:程序博客网 时间:2024/06/06 00:51
  private void getWindowSize(){  handler.post(runnable);  }      Handler handler=new Handler();      Runnable runnable=new Runnable(){@Overridepublic void run() {WindowManager mWm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); Dialog dialog = new Dialog(MainActivity.this); dialog.setTitle("window manager test!");dialog.show();//必须写在show()后 写在show()前无效dialog.setCanceledOnTouchOutside(true);//设置dialog以外能获得焦点//WindowManager win=getWindowManager();//获取屏幕大小//Display display=win.getDefaultDisplay();//int width=display.getWidth();//int height=display.getHeight();//获取屏幕大小 上面那种已过时               DisplayMetrics dm = new DisplayMetrics();               getWindowManager().getDefaultDisplay().getMetrics(dm);               int width=dm.widthPixels;               int height=dm.heightPixels;                Window dialogView=dialog.getWindow();//获取dialog的windowWindowManager.LayoutParams lp=dialogView.getAttributes();lp.width=(int) (width*0.8);//对话框宽高lp.height=(int)(height*0.2);lp.x=-200;//坐标(x,y)=(0,0) 不在左上角 而是在中间!!lp.y=-100;dialogView.setAttributes(lp);}private void setCanceledOnTouchOutside(boolean b) {// TODO Auto-generated method stub}            };
   
0 0