【问题标题】:How to set Alert Dialog height and width in Android?如何在 Android 中设置警报对话框的高度和宽度?
【发布时间】:2011-04-07 06:39:46
【问题描述】:

这是我的代码:

@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. 我需要全屏显示AlertDialog/屏幕尺寸的95%。
  2. 我需要处理Dialog 中的更多字段。
  3. 如何在AlertDialog 中启用HorizontalScrollview

【问题讨论】:

    标签: android fullscreen android-alertdialog horizontal-scrolling


    【解决方案1】:

    要获得滚动行为,请在您的布局中添加一个环绕的 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;
    }
    

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

    【讨论】:

    • 我已经尝试过了。我只能以垂直方式滚动,但我需要以水平方式滚动。当我添加 Horizo​​ntalScroll 它不起作用。然后在设置 LayoutParams 后我得到了仅相同的屏幕尺寸。
    • 如果要水平滚动,请将ScrollView替换为HorizontalScrollView,当达到最大宽度时会自动添加水平滚动。但是,目前没有允许您同时水平和垂直滚动的小部件(google it)。但对话框会尽可能垂直拉伸。
    • 谢谢。我得到了水平滚动。但我需要全屏显示警报。
    • 就我的尝试而言,除了让它自行伸展之外,我没有找到其他解决方案......抱歉,我现在没有帮助。如果你真的需要全屏,可以尝试创建一个新的 Activity。
    • 感谢 Michael Rose 的帮助。
    【解决方案2】:

    我不确定这一点,但您可以设置自己的对话框布局。参考我的回答Android: Dialog dismisses without calling dismiss

    【讨论】:

    • 在上面的代码中,我可以设置自己的布局,但警报的大小非常小。我需要以全屏/指定大小的布局显示
    • 您可以在 xml 中指定大小。是吗?
    • 对于屏幕设计,我只使用 Java。但这里我也指定了 LinearLayout 的大小...
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      相关资源
      最近更新 更多