2012-05-03 176 views
1

我想显示警报对话框设备的屏幕大小。我通过这个链接得到了解决方案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); 

如何设置消息,以便它能正确显示在对话窗口中?

在此先感谢 普什帕

+0

它在我的手机中占据了近90%的份额。你打给谁的警报对话框的父母是什么? 。 – Niranjan

+0

确定按钮是正确的,即它位于窗口的底部。唯一的事情是我没有得到建设者类的消息。 – pushpa

回答

0

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

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); 
+0

非常感谢。这很好。但我观察到的是,如果设备方向在对话框位于前面时发生变化,则必须按两次OK按钮才能关闭对话框。如何避免这种情况? – pushpa

+0

@pushpa如果它正在工作,请考虑接受这个解决方案。让我检查一下方向问题。 – Niranjan

+0

@ ngen:谢谢,即使重力居中,对话框也会出现在顶部窗口。如何使对话中心。 – pushpa

1

通过默认情况下采用离子不能设置文字大小。你可以做一件简单的事情。 请求一个XML并将该XML充满并传递给构建器视图。

builder.setView(view) 

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

+0

谢谢santhu,这很好。但是,如果设备方向发生变化,当对话框在前面时,我们应该按两次确定按钮(与我们的方向更改次数一样)以关闭对话框。 – pushpa