自定義Dialog_位置 座標

	  private void getWindowSize(){
		  handler.post(runnable);
	  }
      Handler handler=new Handler();
      Runnable runnable=new Runnable(){

		@Override
		public 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的window
			WindowManager.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
			
		}
    	  
      };
   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章