2012-09-14 19 views
9

我有以下XML佈局:這應該LinearLayout中使用Android:layout_height = 「WRAP_CONTENT」?

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

    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" // ==> here I get the error. 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Test" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="5dp" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="2dip" 
     android:background="#298EB5" 
     android:orientation="horizontal" /> 


    </LinearLayout> 
</ScrollView> 

但我得到的皮棉消息:

這LinearLayout中應該使用Android:layout_height = 「WRAP_CONTENT」

爲什麼我收到這條信息?

回答

10

LinearLayout的設計用於堆疊元素並排或在彼此的頂部。我的猜測是,這皮棉警告建議virtical堆疊由於ScrollView

Documentation:一個LinearLayout中的

「所有的孩子都堆疊一前一後,所以一個垂直列表只會有每行的一個孩子,無論多麼廣泛他們正在和一個水平列表將會只有一個行高(最高孩子的身高,加上填充值)。一個LinearLayout中每個孩子和重力(右,中心或左對齊)之間的尊重利潤率兒童。」

+3

感謝老兄進行格式化。 –

3

其皮棉警告你應該使用

android:layout_height="wrap_content" 

wrap_content佔據高度爲每附加要求的內容。 這裏根據要求的佈局包裝的高度

4

這不是一個錯誤,但它不建議,因爲它會在某些情況下產生不希望的結果。在使用scrollview時,我遵循Romain的article。我希望這將解釋消息的原因。

相關問題