自定义警报对话框未在Android上垂直居中

时间:2010-07-19 12:16:24

标签: android dialog alert

我制作了一个自定义警告对话框,以便在游戏结束时显示,以便玩家可以输入其名称进行保存。问题是当我在对话框上调用show()但它没有垂直居中时!无论我在xml中设置什么属性或使用setGravity(),它都应该低于应有的值。

我认为这与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

11 个答案:

答案 0 :(得分:3)

该错误描述为here。即使没有标题或图标,AlertDialog也会为标题/图标面板保留空间。

我认为修复非常简单:它应该将顶部面板布局设置为GONE,就像没有按钮时的按钮面板一样。在此之前,唯一的解决方法是实现自己的Dialog子类。

答案 1 :(得分:2)

您可以为此使用“活动”而不是自定义提醒。您必须在android清单文件中将活动主题设置为对话框:

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

您可以根据需要调整活动xml布局。

答案 2 :(得分:2)

如果您实现自己的对话框,行 requestWindowFeature(Window.FEATURE_NO_TITLE)会隐藏标题面板,对话框将在屏幕中居中。也许它也适用于AlertDialog。

答案 3 :(得分:1)

机器人:主题=&#34; @android:风格/ Theme.Dialog&#34;

答案 4 :(得分:0)

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

答案 5 :(得分:0)

不是真正的答案,但我遇到了类似的问题。看起来它是居中的,但假设AlerterDialog有一个标题集。在我的情况下,我只是设置了一个标题。

答案 6 :(得分:0)

您可以尝试使用AlertDialog.Builder.setCustomTitle(View);代替setView吗?我们使用它是因为警报对话框看起来比带有空标题的对话框要好一些。

答案 7 :(得分:0)

你应该在xml中使用以下行     首先从xml中删除该填充行,然后删除

android:layout_gravity="center"

在此之后,您的对话框将显示在中央,如果您使用左边距等,则删除它,如果您正在使用

android:layout_width="fill_parent"

而不是用

更改它
android:layout_width="wrap_content"

之后,您的对话框将出现在中心。

答案 8 :(得分:0)

尝试:

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

答案 9 :(得分: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();

答案 10 :(得分:0)

如果你正在处理任何高度和宽度属性,你必须确保不要改变高度,因为它会改变位置,这是一个例子。

     myProgressDialog.show();           
            float widthPecent = 0.60f;
            //order matters
            DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            int displayWidth = displayMetrics.widthPixels;

        //dont do any adjustments to the height. **************************  <<<<<<<<<<  

            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
    
            layoutParams.copyFrom(myProgressDialog.getWindow().getAttributes());
            int dialogWindowWidth = (int) (displayWidth * widthPecent);
            layoutParams.width = dialogWindowWidth;

 //dont do any changes to the height. **************************  <<<<<<<<<<  

            myProgressDialog.getWindow().setAttributes(layoutParams);

layoutParams.height = dialogWindowHeight; //注释或删除此行。

相关问题