java 如何设置对话框的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6979271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set dialog's position?
提问by Roger
I have made a custom dialog.
我做了一个自定义对话框。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myBackgroundStyle"
parent="@android:style/Theme.Translucent.NoTitleBar" />
</resources>
Dialog dialog = new Dialog(this, R.style.myBackgroundStyle);
dialog.setContentView(R.layout.dialog);
dialog.show();
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.y = 225; params.x = 225;
params.gravity = Gravity.TOP | Gravity.LEFT;
dialog.getWindow().setAttributes(params);
But the problem is that it appears in the top left corner and I can't find a way to place it where I need it. params.y=225; params.x=225;
somehow don't affect it.
但问题是它出现在左上角,我找不到将它放在我需要的地方的方法。params.y=225; params.x=225;
不知何故不影响它。
Any ideas?
有任何想法吗?
edit: If I have the xml like that ( style/Theme.Dialog ), then the parameters and location work fine, but a modal shadow appears. Is there a way to remove it?
编辑:如果我有这样的 xml ( style/Theme.Dialog ),那么参数和位置工作正常,但会出现模态阴影。有没有办法去除它?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myBackgroundStyle" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
</style>
</resources>
采纳答案by Felix
Try using Theme.Panel
as the style's parent
instead.
尝试使用Theme.Panel
作为样式的parent
代替。
回答by Felix
Try creating a new set of parameters:
尝试创建一组新参数:
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.y = 225; params.x = 225;
params.gravity = Gravity.TOP | Gravity.LEFT;
dialog.getWindow().setAttributes(params);
Edit:if you want to preserve the window's attributes, you could try adding this as the second line:
编辑:如果要保留窗口的属性,可以尝试将其添加为第二行:
params.copyFrom(dialog.getWindow().getAttributes());
However, note that the copyFrom
method is completely undocumented, so I have no idea if it does what it sounds like it does.
但是,请注意该copyFrom
方法完全没有记录,所以我不知道它是否听起来像它所做的那样。
回答by Michael
not 100% sure, but is it something to do with params.gravity=Gravity.TOP | Gravity.LEFT;
?
不是 100% 肯定,但这与它有关params.gravity=Gravity.TOP | Gravity.LEFT;
吗?
another edit
另一个编辑
ok then, this one should be of more use: Calling android dialog without it fading the background
好吧,这个应该更有用:调用android对话框而不会使背景褪色
回答by Michael
In the above code the line...
在上面的代码行...
dialog.show();
Should appear after
应该出现在
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.y = 225; params.x = 225;
params.gravity = Gravity.TOP | Gravity.LEFT;
dialog.getWindow().setAttributes(params);
...
By the looks of things you are showing the dialog first then setting x,y coordinates after the dialog has already been drawn to the screen.
从外观上看,您首先显示对话框,然后在对话框已绘制到屏幕后设置 x,y 坐标。
回答by herbertD
Just remove params.gravity = Gravity.TOP | Gravity.LEFT; and it will works. I've tried.
只需删除 params.gravity = Gravity.TOP | 重力.LEFT; 它会起作用。我试过了。