Android 上的自定义警报对话框未垂直居中
我制作了一个在游戏结束时显示的自定义警报对话框,以便玩家可以输入其名称来保存它。问题是当我调用 show()
时,对话框出现,但它不是垂直居中的!无论我在 xml 中设置什么属性或使用 setGravity()
时,它都比应有的值低一点。
感谢您的帮助。
有关更多详细信息,这是我的代码:
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));
builder = new AlertDialog.Builder(this);
builder.setView(layout);
newRecDialog = builder.create();
这是 newrecord.xml
的 XML 布局的第一个元素的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:padding="10dp"
android:baselineAligned="true">
这是输出屏幕截图:
(来源:free.fr)
I made a custom alert dialog box to be displayed at the end of my game so that the player can enter its name to save it. The problem is when I call show()
on the dialog appears but it's not vertically centered! It's a bit lower than it should and no matter what properties I set in the xml or when using setGravity()
.
I think this is the same problem as the one mentioned here, but no one gave a proper answer.
Thanks for your help.
For more details, here is my code:
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));
builder = new AlertDialog.Builder(this);
builder.setView(layout);
newRecDialog = builder.create();
And here is the code of the first element of the XML layout of newrecord.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:padding="10dp"
android:baselineAligned="true">
Here is the output screenshot:
(source: free.fr)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
此处描述了该错误。即使没有标题也没有图标,AlertDialog 也会为标题/图标面板保留空间。
我认为修复方法非常简单:它应该将顶部面板布局设置为“消失”,就像在没有按钮的情况下对按钮面板所做的那样。在此之前,唯一的解决方法是实现您自己的 Dialog 子类。
The bug is described here. The AlertDialog is reserving space for the title/icon panel even where there is neither a title nor an icon.
The fix is, I think, quite simple: it should set the top panel layout to GONE, just as it does for the button panel in the event of there being no buttons. Until that's done, the only workaround is to implement your own Dialog subclass.
如果您实现自己的对话框,则 requestWindowFeature(Window.FEATURE_NO_TITLE) 行会隐藏标题面板,并且对话框位于屏幕中央。也许它也适用于 AlertDialog。
If you implement your own dialog the line requestWindowFeature(Window.FEATURE_NO_TITLE) hides the title panel and the dialog is centered on the screen. Maybe it works with a AlertDialog too.
为此,您可以使用活动而不是自定义警报。您必须在 android 清单文件中将 Activity 的主题设置为对话框:
并且您可以根据需要调整 Activity xml 布局。
You can use an Activity instead of a custom alert for this. You have to set the theme of activity as dialog in the android manifest file:
And you can adjust the activity xml layout as per your need.
android:theme="@android:style/Theme.Dialog"
android:theme="@android:style/Theme.Dialog"
如果您正在处理任何高度和宽度属性,则必须确保不要更改高度,因为它会改变位置,这是一个示例。
布局参数.height = 对话框窗口高度; //注释或删除此行。
If you are dealing with any height and width attributes, you must make sure not to alter the height, since it will alter the position, here is a sample.
layoutParams.height = dialogWindowHeight; //comment or remove this line.
将 AlertDialog 上的属性设置为
android:gravity="center"
或以编程方式设置为setGravity(Gravity.CENTER)
。此重力仅适用于您的布局,不适用于手机的显示。如果您使用自定义标题,它看起来不像中心垂直。set the attribute on your AlertDialog as
android:gravity="center"
or programmatically assetGravity(Gravity.CENTER)
. This Gravity is for your layout only not for the display of your mobile. if you use Custom Title its did not look like center vertical.不是真正的答案,但我遇到了类似的问题。它看起来是居中的,但它假设 AlerterDialog 有一个标题集。就我而言,我只是设置了一个标题。
Not really an answer, but I was having a similar issue. It appears that it is centered, but it is assuming the AlerterDialog has a title set. In my case, I just set a title.
您可以尝试使用
AlertDialog.Builder.setCustomTitle(View);
而不是setView
吗?我们使用它是因为警报对话框看起来比带有空标题的对话框好一点。Can you try using
AlertDialog.Builder.setCustomTitle(View);
instead ofsetView
? We use it because alert dialog looks a bit better than dialog with empty title.您应该在 xml 中使用以下行
首先从您的 xml 中删除该填充行,然后
您的对话框将出现在中心,如果您使用左侧的边距等,则删除它,如果您正在使用它,则将
其更改为
您的对话框将是出现在中心。
you should use the following line in the xml
firstly remove that padding line from your xml and after that
after this your dialog will appear in center and if you are using the margin from left etc than remove that and also if you are using that
than change it with the
after that your dialog will be appear in the center.
尝试:
Try:
您需要禁用窗口标题。
首先为您的对话框创建自定义样式(确保使用适合您需求的父主题):
然后将您的样式应用到对话框构建器:
You need to disable the window title.
First create a custom style for your dialog (Make sure to use the parent theme that fits your needs):
Then apply your style to the dialog builder:
在设置对话框内容之前调用
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE
)。将dialog
替换为您声明的方式。此解决方案适合我。无需设置重力
。Call
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE
before setting the dialog content. Replacedialog
with how you declared it. This solution works for me. There's no need to set theGravity
.