Android開發之Dialog\popupindow\windowManager的區別及自定義方法詳解

在Android開發中,有三種可以彈出的窗體可以使用,一種是Dialog,一種是popupindow,還有一種是最原始的方法,我估計也是dialog和popupwindow的實現原理,用windowManger添加自定義view窗體。


一、Dialog和popupwindow的區別

這兩者都是彈窗,但有本質上的區別。Dialog是非阻塞式彈窗,Dialog彈出來後,後臺還在做其他事情。而當Popupwindow彈出的時候,程序阻塞,後臺停止運動,只有當我們調用了dismiss方法之後,程序纔會繼續向下執行。



二、Dialog和popupwindow的自定義方法

1、Dialog的自定義方法:

Dialog的自定義方法有幾種,可以繼承Dialog類然後setContentView();這裏主要講解運用AlertDialog.Builder來創建自定義對話框的方法。

直接上代碼:

// 新建一個buidler
		AlertDialog.Builder builder = new Builder(this);
		// 填充自定義view
		View view = View.inflate(this, R.layout.freedom, null);
		// 創建dialog
		final AlertDialog dialog = builder.create();
		// 設定自定義View,後面跟的值必須爲0
		dialog.setView(view, 0, 0, 0, 0);
		// 查找組件
		confirm = (Button) view.findViewById(R.id.confirm);
		cancle = (Button) view.findViewById(R.id.cancle);
		password = (EditText) view.findViewById(R.id.password);
		passwordConfirm = (EditText) view.findViewById(R.id.password_confirm);
		// 組件設定動作監聽
		confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String pwd = password.getText().toString().trim();
				String pwd_confirm = passwordConfirm.getText().toString()
						.trim();
				if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwd_confirm)) {
					Toast.makeText(getApplicationContext(), "", 0).show();
					return;
				}
				if (!pwd.equals(pwd_confirm)) {
					Toast.makeText(getApplicationContext(), "", 0).show();
					return;
				}
				Editor editor = sp.edit();
				editor.putString("password", Md5Utils.encode(pwd));
				editor.commit();
				dialog.dismiss();
			}
		});
		cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
		// 可以不設定一下屬性,默認是包裹自定義View組件的大小
		// LayoutParams attributes = dialog.getWindow().getAttributes();
		// attributes.gravity = Gravity.CENTER;
		// attributes.width = 100;
		// attributes.height = 100;
		// dialog.getWindow().setAttributes(attributes);

其實在create之後,還可以這樣定義:

 final AlertDialog dialog = new AlertDialog.Builder(this).create();
		 dialog.show();
		 Window window = dialog.getWindow();
		        // *** 主要就是在這裏實現這種效果的.
		        // 設置窗口的內容頁面,<span style="font-family: Arial, Helvetica, sans-serif;">freedom</span><span style="font-family: Arial, Helvetica, sans-serif;">.xml文件中定義view內容</span>
		 window.setContentView(R.layout.freedom);
		        // 爲確認按鈕添加事件,執行退出應用操作
		 ImageButton confirm = (ImageButton) window.findViewById(R.id.confirm);
		 confirm.setOnClickListener(new View.OnClickListener() {
		  public void onClick(View v) {
		  
		  }
		 });
		 
		        // 關閉alert對話框架
		        ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
		        cancel.setOnClickListener(new View.OnClickListener() {
		   public void onClick(View v) {
			   dialog.cancel();


2、PopUpwindow

這個比Dialog的使用要方便得很多。直接上代碼:

/**
	 * @Title: showPopWindow
	 * @Description: 彈窗
	 * @param view 自定義View視圖
	 * @param x  寬
	 * @param y  高
	 * @param v  掛載窗體
	 * @param gravity 顯示方位
	 * @param atX  X方向偏移量
	 * @param atY  Y方向偏移量
	 * @param isClose  是否設定爲點擊窗體外部,窗體自動關閉
	 * @return
	 * @throws
	 */
	public PopupWindow showPopWindow(View view, int x, int y, View v,
			int gravity, int atX, int atY, boolean isClose) {
		PopupWindow popupWindow = new PopupWindow(view, DptoPxUtil.dip2px(
				context, x), DptoPxUtil.dip2px(context, y), true);
		if (isClose) {
			popupWindow.setBackgroundDrawable(new BitmapDrawable());
		}
		popupWindow.setAnimationStyle(R.style.round_container);
		popupWindow.showAtLocation(v, gravity, atX, atY);

		return popupWindow;

	}

除了showAtlocation外,我們也可以設定它在展示在某一個空間的下方,調用方法popupWindow.showAsDropDown(anchor, xoff, yoff, gravity)即可。


3、WindowManger

調用windowMnager添加窗體,應該是所有彈窗的實現原理,直接上代碼:

rrentView = LayoutInflater.from(context).inflate(
				R.layout.round_container, null);
windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		layoutParams = new WindowManager.LayoutParams();
		layoutParams.horizontalMargin = layoutParams.verticalMargin = 0;
		layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
		layoutParams.x = x;
		layoutParams.y = y;
		layoutParams.width = width;
		layoutParams.height = height;
		layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
		layoutParams.format = PixelFormat.TRANSLUCENT;
		layoutParams.windowAnimations = 0;
		windowManager.addView(currentView, layoutParams);
			
			
		//windowManager.updateViewLayout(currentView, layoutParams);//更新當前view的位置


關於三種彈窗方式的動畫效果已經在昨天講過,今天就不在描述。

希望能幫助到看到此文章的人。






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章