自定义警报对话框未在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">
这是输出截图: alt text http://the.niox.free.fr/divers/dialog_offset.png     
已邀请:
这里描述了这个bug。即使没有标题或图标,AlertDialog也会为标题/图标面板保留空间。 我认为,修复非常简单:它应该将顶部面板布局设置为GONE,就像没有按钮时的按钮面板一样。在此之前,唯一的解决方法是实现自己的Dialog子类。     
您可以使用“活动”而不是自定义警报。您必须在android清单文件中将活动主题设置为对话框:
android:theme="@android:style/Theme.Dialog"
您可以根据需要调整活动xml布局。     
如果您实现自己的对话框,行requestWindowFeature(Window.FEATURE_NO_TITLE)会隐藏标题面板,并且对话框在屏幕上居中。也许它也适用于AlertDialog。     
机器人:主题= “@安卓风格/ Theme.Dialog”     
将AlertDialog上的属性设置为
android:gravity="center"
或以编程方式设置为
setGravity(Gravity.CENTER)
。此重力仅适用于您的布局,不适用于移动设备的显示。如果您使用自定义标题,它看起来不像中心垂直。     
不是真正的答案,但我遇到了类似的问题。它似乎是居中的,但假设AlerterDialog有一个标题集。在我的情况下,我只是设置了一个标题。     
你能尝试使用
AlertDialog.Builder.setCustomTitle(View);
而不是
setView
吗?我们使用它是因为警报对话框看起来比带有空标题的对话框要好一些。     
你应该在xml中使用以下行     首先从xml中删除该填充行,然后再删除
android:layout_gravity="center"
在此之后,您的对话框将显示在中央,如果您使用左边距等,则删除它,如果您正在使用它
android:layout_width="fill_parent"
而不是改变它
android:layout_width="wrap_content"
之后,您的对话框将出现在中心。     
尝试:
newRecDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
    
您需要禁用窗口标题。 首先为对话框创建自定义样式(确保使用符合您需求的父主题):
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
   <item name="android:windowNoTitle">true</item>
</style>
然后将您的样式应用于对话框构建器:
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( new ContextThemeWrapper(this, R.style.CustomDialog));
builder.setView(layout);

newRecDialog = builder.create();
    

要回复问题请先登录注册