繁体   English   中英

如何在Android中设置警报对话框的高度和宽度?

[英]How to set Alert Dialog height and width in Android?

这是我的代码:

@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;    

    AlertDialog alertChoice = new AlertDialog.Builder(context).create();
    alertChoice.setTitle("Title");
    alertChoice.setView(ViewDialogScreen("Test"));    
    alertChoice.show();    
  }
  private View ViewDialogScreen(String strText) {
    LinearLayout llay = new LinearLayout(context);
    llay.setLayoutParams(new LayoutParams(320, 400));
    TextView tv = new TextView(context);
    tv.setText(strText);
    llay.addView(tv);
    return llay;
  }

在此处输入图片说明

我得到了上面的输出。

  1. 我需要全屏显示/显示屏幕尺寸的95%的AlertDialog
  2. 我需要在Dialog处理更多字段。
  3. 如何在AlertDialog启用HorizontalScrollview

要获得滚动行为,请在布局中添加一个周围的ScrollView ,这将使您的ViewDialogScreen方法变为:

private View ViewDialogScreen(String strText) {
    ScrollView scroll = new ScrollView(context);
    scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    LinearLayout llay = new LinearLayout(context);
    llay.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    TextView tv = new TextView(context);
    tv.setText(strText);
    scroll.addView(llay);
    llay.addView(tv);
    return scroll;
}

现在尝试添加更多字段,它应该会滚动。

我不确定,但是您可以为对话框设置自己的布局。 请参阅我的答案Android:对话框关闭而不调用dismiss

如果要自定义AlertDialog ,则需要创建一个Custom Dialog

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM