2011-07-19 65 views
9

我知道這肯定是一個簡單的問題......我只是不知道如何解決它。如何讓視圖填充可用的空間?

左右(只是一個例子),

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:orientation="vertical"> 
    <Button  android:id="@+id/fakeButton" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent"/> 
    <Button  android:id="@+id/saveSearch" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/saveSearch"/> 
</LinearLayout> 

這是行不通的 - 第一個按鈕將使第二無形。重量會讓所有的比例都不是我想要的。

我是厚厚的嗎?

編輯:我不知道它,但它似乎是順序是重要的(從答案)。加載佈局是迭代的而不是整體的。您可以將第一個元素設置爲固定高度,剩下的元素將填充剩下的元素。但我需要的是讓最終的元素達到一個固定的高度。

回答

11

您可以準確地使用layout_weight爲:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:orientation="vertical"> 
    <Button  android:id="@+id/fakeButton" 
       android:layout_height="1" 
       android:layout_width="match_parent" 
       android:layout_weight="1.0" /> 
    <Button  android:id="@+id/saveSearch" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/saveSearch"/> 
</LinearLayout> 

有了這個,你將有底部按鈕具備的基本高度,fakeButton佔用所有剩餘空間。

layout_height控制剩餘空間應該如何在不同視圖之間進行拆分。默認值是0(不佔用任何額外的空間)。如果您將兩個按鈕放在相同的高度和1.0的權重下,則每個按鈕將佔用50%的空間。

+0

我不明白這是如何工作的(儘管不要誤解我的意思 - 它/是/正在工作)。你能否給出更詳細的解釋? – Graeme

+1

啊 - 如果LinearLayout中的一個孩子有一個layout_weight,那麼它的寬度(水平方向)或高度(垂直方向)將被忽略。相反,它會將其容器填充到體重爲0的其他兒童或體重> 0的其他兒童的程度,他們將通過比較體重值來分享容器的百分比。 – Graeme

+3

你實際上需要考慮這個系統2次:首先,忽略重量,通常做佈局。如果LinearLayout中有額外的空間,並且有些孩子的體重> 0,那麼給體重> 0的兒童提供額外空間的一部分(水平和/或垂直),與體重總和成比例。 – Gregory

-1

應該是其他方式..
首先你應該有固定的元素,然後將match_parent或FILL_PARENT

0

我認爲它幫助你..

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:scaleType="center" 
android:background="@drawable/bg" 
> 

<Button 
android:id="@+id/button" 
android:layout_width="wrap_content" 
android:layout_height="60dip" 
android:text="button" 

/> 
<Button 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="fill_parent" 
android:layout_weight="1" 
android:layout_below="@+id/button" 
android:text="button1" 

/> 


</RelativeLayout> 
+0

這僅適用於頂部視圖首先放置。 – Graeme

2

我想你想讓fakeButton填補休息空間吧?您可以使用android:layout_weight來執行此操作。這裏有一個例子:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:orientation="vertical"> 
<Button  android:id="@+id/fakeButton" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_weight="1"/> 
<Button  android:id="@+id/saveSearch" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/saveSearch"/> 
</LinearLayout> 
2

另一個很簡單的解決方案。只需使用FrameLayout和邊距屬性:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <Button  android:id="@+id/fakeButton" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent" 
       android:text="Fake button" 
       android:layout_marginBottom="50dip" /> 

    <Button  android:id="@+id/saveSearch" 
       android:layout_gravity="bottom" 
       android:layout_width="fill_parent" 
       android:layout_height="50dip" 
       android:text="Save search"/> 
</FrameLayout> 
+1

有趣的做法! – Graeme