閱讀916 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android中PopupWindow自定義坐標實現

Android中PopupWindow位置的確定一般通過showAsDropDown函數來實現,該函數有兩個重載函數,分別定義如下:

  1. public void showAsDropDown(View anchor) {  
  2.     showAsDropDown(anchor, 00);  
  3. }  
  4.   
  5. public void showAsDropDown(View anchor, int xoff, int yoff) {  
  6.     if (isShowing() || mContentView == null) {  
  7.         return;  
  8.     }  
  9.   
  10.     registerForScrollChanged(anchor, xoff, yoff);  
  11.   
  12.     mIsShowing = true;  
  13.     mIsDropdown = true;  
  14.   
  15.     WindowManager.LayoutParams p = createPopupLayout(anchor.getWindowToken());  
  16.     preparePopup(p);  
  17.   
  18.     updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff));  
  19.   
  20.     if (mHeightMode < 0) p.height = mLastHeight = mHeightMode;  
  21.     if (mWidthMode < 0) p.width = mLastWidth = mWidthMode;  
  22.   
  23.     p.windowAnimations = computeAnimationResource();  
  24.   
  25.     invokePopup(p);  
  26. }  
    public void showAsDropDown(View anchor) {
        showAsDropDown(anchor, 0, 0);
    }

    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if (isShowing() || mContentView == null) {
            return;
        }

        registerForScrollChanged(anchor, xoff, yoff);

        mIsShowing = true;
        mIsDropdown = true;

        WindowManager.LayoutParams p = createPopupLayout(anchor.getWindowToken());
        preparePopup(p);

        updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff));

        if (mHeightMode < 0) p.height = mLastHeight = mHeightMode;
        if (mWidthMode < 0) p.width = mLastWidth = mWidthMode;

        p.windowAnimations = computeAnimationResource();

        invokePopup(p);
    }

也就是說,調用第一個函數時,x和y坐標偏移量默認是0,此時PopupWindow顯示的結果如下中圖所示。而要實現PopupWindow顯示在wenwen的正下方時,就需要程序員自己進行坐標偏移量的計算,下右圖所示,當點擊wenwen時,PopupWindow顯示在正下方,這正是我們所需要的,對稱是一種美啊。




代碼實現的關鍵是點擊wenwen後的響應函數,此處直接上代碼,不廢話了:

  1. public void onClick(View v) {  
  2.     LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
  3.     ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(  
  4.             R.layout.tips, nulltrue);  
  5.     pw = new PopupWindow(menuView, LayoutParams.WRAP_CONTENT,  
  6.             LayoutParams.WRAP_CONTENT, true);    
  7.     // 設置點擊返回鍵使其消失,且不影響背景,此時setOutsideTouchable函數即使設置為false   
  8.     // 點擊PopupWindow 外的屏幕,PopupWindow依然會消失;相反,如果不設置BackgroundDrawable   
  9.     // 則點擊返回鍵PopupWindow不會消失,同時,即時setOutsideTouchable設置為true   
  10.     // 點擊PopupWindow 外的屏幕,PopupWindow依然不會消失   
  11.     pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));   
  12.     pw.setOutsideTouchable(false); // 設置是否允許在外點擊使其消失,到底有用沒?   
  13.     pw.setAnimationStyle(R.style.PopupAnimation); // 設置動畫   
  14.   
  15.     // 計算x軸方向的偏移量,使得PopupWindow在Title的正下方顯示,此處的單位是pixels   
  16.     int xoffInPixels = ScreenTools.getInstance(PopDemoActivity.this).getWidth() / 2 - titleName.getWidth() / 2;  
  17.     // 將pixels轉為dip   
  18.     int xoffInDip = ScreenTools.getInstance(PopDemoActivity.this).px2dip(xoffInPixels);  
  19.     pw.showAsDropDown(titleName, -xoffInDip, 0);  
  20.     //pw.showAsDropDown(titleName);   
  21.     pw.update();  
  22.   
  23.     TextView tv = (TextView) menuView.findViewById(R.id.tips_ok);  
  24.     tv.setOnClickListener(new View.OnClickListener() {  
  25.   
  26.         public void onClick(View v) {  
  27.             pw.dismiss();  
  28.         }  
  29.   
  30.     });  
  31.       
  32. }  
public void onClick(View v) {
	LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
	ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(
			R.layout.tips, null, true);
	pw = new PopupWindow(menuView, LayoutParams.WRAP_CONTENT,
			LayoutParams.WRAP_CONTENT, true);  
	// 設置點擊返回鍵使其消失,且不影響背景,此時setOutsideTouchable函數即使設置為false
	// 點擊PopupWindow 外的屏幕,PopupWindow依然會消失;相反,如果不設置BackgroundDrawable
	// 則點擊返回鍵PopupWindow不會消失,同時,即時setOutsideTouchable設置為true
	// 點擊PopupWindow 外的屏幕,PopupWindow依然不會消失
	pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
	pw.setOutsideTouchable(false); // 設置是否允許在外點擊使其消失,到底有用沒?
	pw.setAnimationStyle(R.style.PopupAnimation); // 設置動畫

	// 計算x軸方向的偏移量,使得PopupWindow在Title的正下方顯示,此處的單位是pixels
	int xoffInPixels = ScreenTools.getInstance(PopDemoActivity.this).getWidth() / 2 - titleName.getWidth() / 2;
	// 將pixels轉為dip
	int xoffInDip = ScreenTools.getInstance(PopDemoActivity.this).px2dip(xoffInPixels);
	pw.showAsDropDown(titleName, -xoffInDip, 0);
	//pw.showAsDropDown(titleName);
	pw.update();

	TextView tv = (TextView) menuView.findViewById(R.id.tips_ok);
	tv.setOnClickListener(new View.OnClickListener() {

		public void onClick(View v) {
			pw.dismiss();
		}

	});
	
}


最後更新:2017-04-02 16:48:08

  上一篇:go Android 4.0 分頁指示器 Android ViewPagerIndicator
  下一篇:go Oracle中序列的操作以及使用前對序列的初始化