2010-09-27 61 views
4

我正在使用包含文本字段,圖像和按鈕的自定義對話框。文本可以包含HTML。有時當文本足夠長時,對話框的底部會從底部切掉。我怎樣才能防止這一點?我想讓Android確定對話框的大小,但似乎並沒有這樣做。在這種情況下,我需要自己調整對話框大小嗎?Android沒有調整足夠大的自定義對話框

這裏是佈局...

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/alert_root_incorrect" 
    style="@style/AlertDialogTheme" 
    android:background="@drawable/rounded_alert" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
> 
    <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/rounded_alert" 
    > 

    <TableLayout 
     android:stretchColumns="0" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
    > 

     <TableRow> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="left" 
      android:textStyle="bold" 
      android:text="Sorry, that's wrong!" 
      android:textColor="@color/gray_dark" /> 

     <ImageView 
      android:id="@+id/check" 
      android:background="@drawable/xmark" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:layout_marginRight="10dp" /> 

     </TableRow> 
    </TableLayout> 

    <TextView 
     android:id="@+id/alert_text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:ellipsize="none" 
     android:text="In fact, this is where the explanation will go. Something about how this passage related to the topic" 
     android:textColor="#000000" /> 

    <Button 
     android:id="@+id/okay_button" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_gravity="center_horizontal" 
     android:background="@drawable/rounded_alert_button" 
     android:text="Okay" 
     android:textColor="@color/white" 
     android:textSize="20sp" /> 
    </LinearLayout> 
</LinearLayout> 

這裏是我使用加載它的代碼...

if (null == layout) { 
    this.layout = inflater.inflate(R.layout.alert_incorrect, (ViewGroup) findViewById(R.id.alert_root_incorrect)); 
} 

TextView message = (TextView) layout.findViewById(R.id.alert_text); 
message.setText(Html.fromHtml(card.getConclusion())); 
((Button) layout.findViewById(R.id.okay_button)).setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     dismissDialog(INCORRECT_DIALOG); 
     nextQuestion(); 
    } 
}); 

layout.requestLayout(); 

dialog = new Dialog(this, R.style.AlertDialogTheme); 
dialog.setContentView(layout); 
dialog.setCancelable(false); 

return dialog; 

下面是我的意思瞬間..

alt text

謝謝,

約翰

+1

它可能是你選擇的'style'有一些大小的限制,這是阻止你的對話框的大小。嘗試沒有風格,看看是否有區別。帶'layout_weight'的'LinearLayout'比'TableLayout'容易,爲什麼只有兩個'LinearLayout'嵌套。在可能的情況下消除「額外」視圖,它們可能會干擾佈局。在設置'textSize'時,我也遇到了調整'Button'的大小問題。你可以嘗試從'Button'中刪除填充。如果您的背景是9修補程序,那麼保留設置也可能存在問題。 – 2011-10-21 15:09:04

回答

1

這並不完美,但我已經通過設置對話框相對於默認顯示的佈局來糾正它。

dialog = new Dialog(this, R.style.AlertDialogTheme); 
dialog.setContentView(layout); 
Window window = dialog.getWindow(); 
window.setLayout(
    (int)(window.getWindowManager().getDefaultDisplay().getWidth() * .90), 
    (int)(window.getWindowManager().getDefaultDisplay().getHeight() * .90)); 

dialog.setCancelable(false); 

只要調整「.90」的值,直到它感覺不錯。

+0

工作完美適合我.. – 2015-09-16 13:09:34

0

這裏是解決方案:

  1. 你應該在你的對話框的XML文件之外添加的LinearLayout
  2. 然後設置這個LinearLayout中的重心爲「中心」
  3. 的最後一個步驟是創建一個LayoutParams並將其設置爲對話框

    android.view.WindowManager.LayoutParams lp = new android.view.WindowManager.LayoutParams(); lp.width = android.view.WindowManager.LayoutParams.FILL_PARENT; lp.height = android.view.WindowManager.LayoutParams.FILL_PARENT; alertDialog.getWindow()。setAttributes(lp);