2010-07-19 46 views
12

我在游戏结束时显示了一个自定义警报对话框,以便玩家可以输入其名称以保存它。问题是当我打电话给show()出现对话框,但它不是垂直居中!这比它应该低一点,不管我在xml中设置什么属性或使用setGravity()自定义警报对话框不在Android上垂直居中

我觉得这是同样的问题the one mentioned here,也没有人给一个合适的回答。

感谢您的帮助。

有关详细信息,这里是我的代码:

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

回答

0

将您的AlertDialog上的属性设置为android:gravity="center"或编程设置为setGravity(Gravity.CENTER)。此重力只适用于您的布局,而不适用于您的手机的显示。如果您使用自定义标题,它看起来不像中心垂直。

+0

我想这一切已经它不起作用。我的XML文件实际上是我膨胀的对话框内容的描述,这是错误的吗? – NioX5199 2010-07-19 13:40:58

+0

我无法正确理解你。但是,无论如何,视图的内容不会影响重力。所以请发布你的代码。我会尝试。 – Praveen 2010-07-19 13:47:24

+0

@ NioX5199:你可以发布你的输出的屏幕截图吗? – Praveen 2010-07-19 14:46:31

0

不是一个真正的答案,但我有一个类似的问题。它似乎是居中,但它假定AlerterDialog有一个标题集。就我而言,我只是设定了一个标题。

3

该缺陷被描述here。 AlertDialog即使在没有标题和图标的情况下也为标题/图标面板保留空间。

的解决方法是,我认为,很简单:它应该在顶部面板布局设置为走了,只是因为它在那里是没有按钮的情况下,该按钮面板。在这之前,唯一的解决方法是实现自己的Dialog子类。

+0

顶部面板?你如何设置顶部面板布局? – ThePerson 2012-04-01 21:50:22

2

您可以使用一个活动,而不是自定义提醒。你必须设置活性对话的主题在Android清单文件:

android:theme="@android:style/Theme.Dialog" 

而且,您可以根据自己的需要调整活动XML布局。

0

你可以尝试使用AlertDialog.Builder.setCustomTitle(View);代替setView?我们使用它,因为警告对话框比空标题对话框看起来好一点。

0

你应该使用下面这行中的XML 首先从你的XML删除填充线,之后

android:layout_gravity="center" 
在此之后

您的对话框将会出现在中心,如果你使用的是缘自左等比删除和如果您正在使用

android:layout_width="fill_parent" 

比用

android:layout_width="wrap_content" 
改变

之后,您的对话框将显示在中心。

0

尝试:

newRecDialog.getWindow().getAttributes().gravity = Gravity.CENTER; 
2

如果你实现了自己的对话框行 requestWindowFeature(Window.FEATURE_NO_TITLE)隐藏标题面板和对话框中心的屏幕上。也许它也适用于AlertDialog。

1

android:theme =“@ android:style/Theme.Dialog”

0

您需要禁用窗口标题。 首先创建一个自定义样式为您的对话框(请务必使用适合您的需求父主题):

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