2010-12-01 125 views
6

我正在學習Android對話框,我對什麼決定了它們的高度感到困惑。如果我將這個XML用於我的對話框佈局。 。 。Android自定義對話框高度

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <Button 
    android:id="@+id/AButton" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_marginLeft="10px" 
    android:text="Click Me to Dismiss" 
    /> 
    <TextView android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_toLeftOf="@id/AButton" 
    android:text="Click this button: " 
    /> 
    <ImageView 
    android:id="@+id/an_image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="42px" 
    android:src="@drawable/screw50"> 
    </ImageView>  
    /> 

我得到這個對話框。 。 。

alt text

但Developer.Android.com說......

WRAP_CONTENT,這意味着視圖想要剛好足夠大包圍它的內容(加墊)

...那麼爲什麼對話遠遠高於它所需要的呢?

如果我更換

android:layout_height="200px" 

它使一個正常的短對話,但我不希望使用明確的像素高度(這不是好的做法)的佈局高度。我如何讓對話框的內容足夠大?

回答

7

您的TextView的高度爲fill_parent。這是否有可能與RelativeLayout的高度結合wrap_content將其高度推至最大值?如果將TextView高度更改爲wrap_content,會發生什麼情況?

要添加,the Android SDK表示RelativeLayout的子項不能依賴佈局,如align_parent_bottom。我認爲fill_parent是一個依賴,因此打破了佈局。

1

嗯,這很奇怪。我用<LinearLayout/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical" 
        android:background="@android:color/white"> 
<TextView android:id="@+id/message" 
       android:padding="5dp" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="10dp"/> 
    ... 
    </LinearLayout> 

它包裹我的內容就好了。

+1

是的,我同意LinearLayout沒有這個問題。但是我想使用RelativeLayout,因爲它可以讓我放置。 RelativeLayout有限制嗎? (我應該提到我使用的是Android 2.2,這個問題發生在仿真和實際的手機上。) – 2010-12-01 15:43:00

+0

爲什麼不在LinearLayout中使用RelativeLayout?不,我不知道什麼:( – 2010-12-01 15:43:53

+1

「爲什麼不在LinearLayout中使用RelativeLayout?」我可以嘗試,但它似乎是一個黑客攻擊,沒有進攻。必須有一種方法才能正確工作。 – 2010-12-01 15:45:42

1

我在過去的2天裏一直在解決同樣的問題。最後它可以正常使用我的對話框樣式中的一些屬性。

這裏是我使用的風格:

<style name="SearchFieldSetterDialog" parent="@android:style/Theme.Dialog"> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:gravity">bottom</item> 
</style> 

關鍵attriubutes是:

<item name="android:windowIsFloating">true</item> 
    <item name="android:windowNoTitle">true</item> 

第一個,我想,需要說的窗口管理器,把對話圍繞框架 和第二個取出爲對話標題保留的空間。

這種風格和佈局如下我終於有了它的工作:

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

... 

</LinearLayout> 

我與部份解決的問題是,與android:windowIsFloating attriubute設置好的作爲true和earlyer平臺然後3.0我不能找到一種方法即使layout_width設置爲fill_parent

希望這可以幫助某人... ...使對話框witdh填滿屏幕和我的英文不好的英文版