2011-05-26 233 views
4

我有一個使用我在XML文件中定義的佈局的自定義樣式的對話框。我希望這個對話框以縱向模式填充屏幕的寬度,但只有橫向模式下的大約400dip。 MaxWidth似乎是完美的方式,但我不知道如何將MaxWidth分配給對話框樣式或XML佈局。如何設置Android對話框的MaxWidth?

回答

-2

你試過設置android:minWidthandroid:minHeight在您的自定義XML

+15

不,但我不明白minWidth和minHeight如何幫助我設置最大寬度。 – chefgon 2011-05-27 16:16:15

3

假設你使用一個叫做layout/dialog_core.xml未來佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" > 

    //All your dialog views go here.... 

</LinearLayout> 

然後,您可以創建兩個文件:layout/dialog.xmllayout-land/dialog.xml

layout/dialog.xml文件不會限制寬度:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" > 

    <include layout="@layout.dialog_core.xml" /> 

</LinearLayout> 

當你layout-land/dialog.xml應該有寬度的限制:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:maxWidth="400dp" > 

    <include layout="@layout.dialog_core.xml" /> 

</LinearLayout> 

當然,你現在需要在代碼中使用R.layout.dialog佈局。

P.S.我僅以舉例的目的使用LinearLayout。任何佈局視圖都可以。

+0

我試過了,但android:maxWidth沒有出現在LinearLayout的自動完成提議窗口中,所以我認爲我不能使用它。無論如何,我嘗試着把它放到項目中,但是這個屬性沒有效果,對話框仍然延伸到屏幕上。 – chefgon 2011-05-26 18:52:20

+0

@chefgon'android:maxWidth'不是你正在尋找的。你想要的是在縱向模式下android:layout_width =「fill_parent」。按照inazaruk的說法,不要忘記將'dialog-land.xml'放在佈局文件夾'res/layout-land'中。 Android將根據屏幕方向自動加載正確的佈局('dialog.xml'或'dialog-land.xml')。 – 2011-07-07 20:03:09

+0

據我所知,根據開發文檔,'dialog-land.xml'也應該命名爲'dialog.xml',但放置在@ resur說的'res/layout-land'中 - 我相信@inazaruk在他的解釋中對他們進行了不同的命名。 – ataulm 2012-04-08 18:36:57

0

你可以在這裏找到一個實際的解決方案:setting a max width for my dialog

答案是使用對話的主題活動,但考慮到它是基於Window對象,它應該不會太複雜,以適應它的對話框。

相關問題