gpt4 book ai didi

android - 如何在特定位置显示自定义对话框?

转载 作者:太空狗 更新时间:2023-10-29 16:38:26 24 4
gpt4 key购买 nike

我有一个按钮,单击它会弹出一个对话框。对话框显示在中心。但我想在按钮下方显示它。如何做到这一点?

我也尝试使用弹出窗口。这是代码

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();
}
});

}

最佳答案

我认为你可以通过使用来实现你正在寻找的东西:

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);

或相应地设置 x y 位置并移除重力。

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

或者您可以使用 POPUPWindow 来使用 link它在 ListView 中显示弹出窗口。

关于android - 如何在特定位置显示自定义对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22350327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com