2014-03-12 67 views
1

我有一個按鈕,其單擊彈出一個對話框。對話框正在顯示在中心。但我想顯示它只是按鈕下方。如何做到這一點?如何在特定位置顯示自定義對話框?

我嘗試使用彈出窗口also.Here是代碼

private void showPopup(final Activity context, Point p) 
    { 
     Display display = getWindowManager().getDefaultDisplay(); 
     width = display.getWidth(); // deprecated 
     height = display.getHeight(); // deprecated 

     int popupWidth =width; 
     int popupHeight =height; 

     // Inflate the popup_layout.xml 
     LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup); 
     LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layout = layoutInflater.inflate(R.layout.datepicker_popup, viewGroup); 

     // Creating the PopupWindow 
     final PopupWindow popup = new PopupWindow(context); 
     popup.setContentView(layout); 
     popup.setWidth(popupWidth+p.x); 
     popup.setHeight(popupHeight+p.y); 
     popup.setFocusable(true); 

     // Some offset to align the popup a bit to the right, and a bit down, relative to button's position. 
     int OFFSET_X = 7; 
     int OFFSET_Y = 65; 

     // Clear the default translucent background 
     popup.setBackgroundDrawable(new BitmapDrawable()); 

     // Displaying the popup at the specified location, + offsets. 
     popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y); 



     // Getting a reference to Close button, and close the popup when clicked. 
     Button close = (Button) layout.findViewById(R.id.close); 
     close.setOnClickListener(new OnClickListener() 
     { 
      /* disable(content_view);*/ 
     @Override 
     public void onClick(View v) 
     { 
      popup.dismiss(); 
     } 
     }); 

     } 

回答

4

我想你可以實現你用找什麼:

getLocationOnScreen()API & PopUpWindow組件。

示例代碼可以如下所示。

int[] location = new int[2]; 
counterView.getLocationOnScreen(location); 
final View mView = inflater.inflate(R.layout.xxxx, null, false); 
final PopupWindow popUp = new PopupWindow(mView, Width, Height, false); 
popUp.setTouchable(true); 
popUp.setFocusable(true); 
popUp.setOutsideTouchable(true); 
popUp.showAtLocation(view, Gravity.NO_GRAVITY, location[0], location[1]); 

或改變重力這樣的:

Dialog dlg = <code to create custom dialog>; 

Window window = dlg.getWindow(); 
WindowManager.LayoutParams wlp = window.getAttributes(); 

wlp.gravity = Gravity.BOTTOM; 
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND; 
window.setAttributes(wlp); 

OR相應地設定x和y位置,並移除重力。

window.getAttributes().x = 100; 
window.getAttributes().y = 100; 

或者您也可以使用POPUPWindow使用這個link它在列表視圖中顯示彈出式窗口。

+0

第二個代碼正在爲我工​​作,但我需要的是顯示對話框下方的按鈕,其單擊彈出對話框。如何做到這一點? – Nevaeh

+0

我也試過。請參閱更新後的問題。如何改變窗口的位置? – Nevaeh

+0

檢查此http://rajeshandroiddeveloper.blogspot.in/2013/07/android-popupwindow-example-in-listview.html –

相關問題