2011-04-07 84 views
1

這裏是我的代碼:如何在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; 
    } 

enter image description here

我喜歡上面的輸出。

  1. 我需要以全屏/屏幕尺寸的95%顯示AlertDialog
  2. 我需要處理Dialog中的更多字段。
  3. 如何在AlertDialog中啓用HorizontalScrollview

回答

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

現在嘗試加入一些更多的領域,它應該滾動。

+0

我已經嘗試過了。我只能以垂直方式滾動,但我需要以水平方式滾動。當我添加Horizo​​ntalScroll時它不起作用。然後在設置LayoutParams之後,我只獲得了相同的屏幕尺寸。 – 2011-04-07 07:04:32

+0

如果您想水平滾動,請將'ScrollView'替換爲'Horizo​​ntalScrollView',並在達到最大寬度時自動添加水平滾動。但是,目前沒有小部件允許您在同一時間水平和垂直滾動(谷歌它)。但對話框將盡可能垂直拉伸。 – 2011-04-07 07:17:11

+0

謝謝。我得到了水平滾動。但我需要全屏顯示警報。 – 2011-04-07 07:21:33

0

我不確定這一點,但您可以設置自己的對話框佈局。請參考我的回答Android: Dialog dismisses without calling dismiss

+0

在我上面的代碼中,我可以設置自己的佈局,但Alert的大小非常小。我需要在全屏/指定大小的佈局 – 2011-04-07 06:54:43

+0

中顯示,您可以用xml指定大小。是嗎? – Vivek 2011-04-07 07:13:19

+0

對於屏幕設計我只使用Java.But這裏我還指定了LinearLayout的大小... – 2011-04-07 07:24:24

0

如果您要自定義AlertDialog,您需要創建一個Custom Dialog

+0

感謝您的回覆 – 2011-04-07 07:38:17