2011-02-08 25 views
2

我正在使用android:theme =「@ android:style/Theme.Dialog」的活動使其充當對話框。最接近於原生對話框的行爲:對話框應該填滿屏幕(寬度)還是不對?而且,如果它動態增長,例如,通過輸入填充文本框?Android:對話框的寬度應該是多少?

我的預感是對話框不應該填滿屏幕。我試圖得到這種行爲,但無論我做什麼,在Android 2.2上,對話框寬度似乎填充父項

我的佈局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView android:id="@+id/text" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:autoText="true" 
     android:text="@string/item_delete_dialog_label" 
     android:padding="5dip" /> 

    <LinearLayout android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     style="@android:style/ButtonBar" android:layout_width="fill_parent"> 

     <Button android:id="@+id/yes" android:layout_height="wrap_content" 
      android:text="@string/button_yes" 
      android:layout_width="wrap_content" 
      android:layout_weight="1"/> 

     <Button android:id="@+id/no" android:layout_height="wrap_content" 
      android:text="@string/button_no" 
      android:layout_width="wrap_content" 
      android:layout_weight="1"/> 

    </LinearLayout> 
</LinearLayout> 

我發現關於做一個對話框填滿屏幕寬度的許多問題,但沒有關於使其在寬度換行。我怎樣才能得到這個?

回答

0

ButtonBar樣式使對話框比使用標準按鈕更寬。將LinearLayout寬度更改爲wrap_content不會影響對話框的寬度,因爲它只是填充設置爲wrap_content的主LinearLayout容器 - 更改按鈕樣式絕對會有所作爲。

0

對話框的一般風格就像他們需要包圍他們的孩子一樣大。如果孩子不佔據整個寬度,對話框也不會。

您的問題可能是您的LinearLayout的寬度爲fill_parent。這意味着它將佔用盡可能多的空間。改變wrap_content的寬度,所以它不會佔用更多的空間。或者,如果你的對話真的很簡單,只需使用AlertDialog即可。