How to set Alert Dialog height and width in Android?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 08:33:06

问题


Here is my code:

@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;
  }

I got the output like the above.

  1. I need to show the AlertDialog in full screen / 95% of the Screen size.
  2. I need to handle more fields in the Dialog.
  3. How do I enable the HorizontalScrollview in the AlertDialog?

回答1:


to get the scrolling behaviour, add a surrounding ScrollView to your layout, which would make your ViewDialogScreen method to this:

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;
}

Now try adding some more fields and it should scroll.




回答2:


I am not sure this but you can set your own layout for dialog. Refer my answer Android: Dialog dismisses without calling dismiss




回答3:


If you want to customize an AlertDialog you need to create a Custom Dialog.



来源:https://stackoverflow.com/questions/5576991/how-to-set-alert-dialog-height-and-width-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!