改变 PopupWindow 的重力
我有一个必须显示弹出窗口的按钮。并且弹出窗口必须与按钮的右侧对齐。我就是这样做的。
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick( final View view ) {
if(popup == null) {
final View view = getLayoutInflater().inflate(R.layout.popup, null);
popup = new PopupWindow(view);
}
if(popup.isShowing()) {
popup.dismiss();
} else {
popup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popup.setFocusable(true);
popup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popup.showAsDropDown(button, 0, 0);
}
}
} );
此代码工作正常,但弹出窗口与按钮的左侧对齐。有没有简单的方法来改变 PopupWindow 的重力?
I have a button that must show a popup window. And the popup window must be aligned with the right side of the button. That's how I do it.
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick( final View view ) {
if(popup == null) {
final View view = getLayoutInflater().inflate(R.layout.popup, null);
popup = new PopupWindow(view);
}
if(popup.isShowing()) {
popup.dismiss();
} else {
popup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popup.setFocusable(true);
popup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popup.showAsDropDown(button, 0, 0);
}
}
} );
This code works fine but the popup window is aligned with the left side of the button. Is there an easy way to change gravity of PopupWindow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现的最简单的方法是指定 x 偏移参数:
The easiest way I've found is to specify the x-offset parameter:
您可以尝试从 xml 文件更改它:
You can try to change it from the xml file: