如何在特定位置显示自定义对话框? [英] How to display a custom Dialog box at a specific position?
本文介绍了如何在特定位置显示自定义对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按钮,单击该按钮会弹出一个对话框.该对话框显示在中间.但是我想在按钮的正下方显示它.如何执行此操作?
I have a Button whose click pops up a dialog box.The dialog box is getting displayed at center.But i want to display it just below the button.How to do this ?
我也尝试过使用弹出窗口.这是代码
I tried using popup window also.Here is the code
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();
}
});
}
推荐答案
我认为您可以通过使用以下方法实现所需的目标:
I think you can achieve what you are looking for by using :
getLocationOnScreen()api& PopUpWindow组件.
getLocationOnScreen() api & PopUpWindow component.
示例代码如下.
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);
或相应地设置x y位置并消除重力.
OR set x y position accordingly and remove gravity.
window.getAttributes().x = 100;
window.getAttributes().y = 100;
,否则您可能会使用POPUPWindow
来使用此链接,它将在列表视图中显示弹出窗口.
or your may be use POPUPWindow
to use this link it displayed popup window in listview.
这篇关于如何在特定位置显示自定义对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文