2013-08-18 51 views
6

我想這應該是一個相當容易回答的問題,如果你理解XML佈局比我更好。我似乎沒有得到我在使用match_parent layout_height時的想法。match_parent表現不如預期

我有一個LinearLayout根元素android:orientation =「vertical」。這裏面的LinearLayout我想三個要素: - TextView的 - ListView控件 - TextView的

對於這兩個我設置機器人的TextViews:layout_height =「WRAP_CONTENT」,從而當需要顯示他們將只有一樣高的內容。問題是,我希望一個TextView坐在窗體的頂部,另一個坐在窗體的底部,而ListView填充窗體上可用的任何空間。所以這是我的XML佈局看起來像:

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="30sp" 
    android:text="Top TextView" /> 

<ListView 
    android:id="@+id/listView_Species" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="30sp" 
    android:text="Bottom TextView" /> 

但它不工作。這是我得到的。我已經選擇了ListView,以便它被突出顯示。注意它如何一直延伸到表單底部,將底部的TextView從表單中移出。 XML Layout 1

當我將ListView的layout_height屬性更改爲某個固定值,如180dp時,這就是表單的樣子。我只是發佈這個來證明底部的TextView在那裏,但我仍然不知道如何讓它固定在屏幕的底部,而ListView佔用了剩餘空間,但是在兩個TextView之間。

XML Layout 2

在此先感謝。

回答

7

雖然其他答案嘗試解決您的問題(他們實際上沒有 - 他們建議你做一些事情看起來類似,但在不同設備上可能看起來不一樣),沒有人填補了你對LinearLayouts和match_parent知識的空白。而這些差距非常普遍 - 谷歌的文檔仍然遠遠低於恆星。

首先,視圖如何在LinearLayout中工作?爲了簡單起見,我們來完成繪製LinearLayout的過程,使用orientation="vertical"

  1. 檢查LinearLayout的第一個子項的高度(簡稱LL)。如果高度爲match_parentfill_parent(舊名稱爲同一事物),則視圖的高度被拉伸以填充整個查看區域。如果高度爲wrap_content,則測量視圖所佔的垂直空間並將該空間用於視圖。如果高度爲非零數字,則視圖高度恰好使用許多像素(如果太小,可能會剪輯)。如果高度爲0,請參閱下文。
  2. 將下一個視圖放在以下的視圖中。檢查其高度並相應採取行動。
  3. 繼續所有視圖。如果一個視圖從底部推出,繼續並停止計算,因爲沒有人會看到它或任何後續視圖(假設沒有ScrollView)。
  4. 如果View的高度爲0,請檢查它的gravity。這需要第二遍,存儲所有視圖的嚴重性,然後按比例分配它們的高度。正如你猜測的那樣,第二遍是佈局時間的兩倍,這對於簡單的佈局並不重要。

說明你的例子組成:LL(第一TextView的)的第一個子被測量並且需要一定量的像素。然後你的ListView佔用所有剩餘的空間(通過match_parent)。然後你的第二個TextView根本不會被繪製,因爲它離開屏幕的底部。這幾乎是你觀察到的,但現在你明白了爲什麼。

解決方案:使用RelativeLayout。在這種情況下完美地工作。

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/top_tv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:textSize="30sp" 
     android:text="Top TextView" /> 

    <TextView 
     android:id="@+id/bottom_tv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:textSize="30sp" 
     android:text="Bottom TextView" /> 

    <ListView 
     android:id="@+id/listView_Species" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/top_tv" 
     android:layout_above="@id/bottom_tv" 
     /> 
</RelativeLayout> 

的RelativeLayout的告訴佈局吹氣在頂部繪製第一個TextView的,然後在底部繪製第二TextView的,然後用你的ListView填充空間的其餘部分。我相信這正是你想要的。

歡迎使用Android。你會很好地使用這種模式!

3

使用android:layout_weight來爲最外層佈局內的小部件定義權重。聲明它們的高度爲0dp,然後爲其中的每一個定義android:layout_weight。 其中三個的總重量總和應爲1.根據您的需要,您可以將頂部和底部的TextView定義爲0.1權重,並將List123定義爲0.8

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight = "0.1" 
    android:textSize="30sp" 
    android:text="Top TextView" /> 

<ListView 
    android:id="@+id/listView_Species" 
    android:layout_width="match_parent" 
    android:layout_weight = "0.8" 
    android:layout_height="0dp" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:textSize="30sp" 
    android:layout_weight = "0.1" 
    android:text="Bottom TextView" /> 
+0

我很高興你的答案,但它並不完全給我我想要的。這導致TextViews也拉伸到一定比例的屏幕尺寸。我希望那些被修復,而只有ListView延伸佔據剩餘的空間。上面的Royi爲此發佈瞭解決方案。 –

11

更改ListView高度0dp並添加weight=1
即:

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="30sp" 
    android:text="Top TextView" /> 

<ListView 
    android:id="@+id/listView_Species" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="30sp" 
    android:text="Bottom TextView" /> 
+0

這麼多的解釋。嘆。 –