2014-03-12 66 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 –

相关问题