【问题标题】:Alert message is not displaying in alert dialog box?警报对话框中未显示警报消息?
【发布时间】:2012-05-03 13:15:10
【问题描述】:

我想显示设备屏幕大小的警告对话框。我通过此链接How to make an alert dialog fill 90% of screen size? 获得了解决方案,我使用了那里给出的解决方案,它工作得很好,但我的警报消息以非常小的尺寸显示。我们甚至无法以横向方式识别信息。我使用了以下代码。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.app_description).setPositiveButton(
                    "Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                        }
                    });
            builder.Title(title);

            Dialog d = builder.setView(new View(this)).create();
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(d.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.FILL_PARENT;
            lp.height = WindowManager.LayoutParams.FILL_PARENT;
            d.show();
            d.getWindow().setAttributes(lp);

如何设置消息,使其正确显示在对话窗口中?

提前致谢 普什帕

【问题讨论】:

  • 它在我的手机中几乎占据了 90%。您从中调用的警报对话框的父级是什么? .
  • “确定”按钮恰到好处,即它位于窗口底部。唯一的问题是没有收到设置为构建器类的消息。

标签: android android-layout dialog android-alertdialog


【解决方案1】:

检查此代码并告诉我这是否是您真正想要实现的目标?

 AlertDialog.Builder builder = new AlertDialog.Builder(this);

    // Creates textview
    TextView text = new TextView(this);  
    text.setText("Hello This text");  
    text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    text.setTextSize(20);        
    text.setGravity(Gravity.CENTER);

    //Creates a linearlayout layout and sets it with initial params
    LinearLayout ll = new LinearLayout(this);
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    ll.setGravity(Gravity.CENTER);
    ll.addView(text);  //adds textview to llayout

    builder.setMessage("Title").setPositiveButton(
            "Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            }); 

    Dialog d = builder.setView(ll).create();   

    //Fills up the entire Screen
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(d.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.FILL_PARENT;
    lp.height = WindowManager.LayoutParams.FILL_PARENT;
    d.show();
    d.getWindow().setAttributes(lp);

【讨论】:

  • 非常感谢。这很好用。但我观察到的是,如果对话框在前面时设备方向发生变化,我必须按两次确定按钮才能关闭对话框。如何避免这种情况?
  • @pushpa 如果它有效,请考虑接受它作为解决方案。让我检查一下方向问题。
  • @ngen: 谢谢,对话框出现在顶部窗口,即使重心是中心。如何制作对话中心。
【解决方案2】:

默认情况下,您不能设置文本大小。你可以做一件简单的事情。 编写一个 XML 并扩展该 XML 并传递给构建器视图。

   builder.setView(view)  

除了您将视图设置为对话框之外,什么都没有。 并且该视图将处理所有触摸。您可以在 xml 中指定视图的高度和宽度,包括文本大小。

【讨论】:

  • 谢谢桑杜,这很好用。但问题是如果设备方向改变,当对话框在前面时,我们应该按两次确定按钮(我们的方向改变的次数)以关闭对话框。