2012-05-30 19 views
0

今天我已經玩了一下與LinearLayout中,並已驚訝的結果:關於LinearLayout中,如何控制resize在

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



<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Big Text" 
    android:gravity="center_vertical|center_horizontal" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 


<TextView 
    android:id="@+id/textView2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 


<Button 
    android:id="@+id/button1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:text="Button" 
    /> 

</LinearLayout> 

這是一個簡單的佈局,文本視圖標題,然後在文本觀點我希望它覆蓋所有父級(但底部按鈕佔用的空間)以及一個放置在底部按鈕layout_gravity =「bottom」的按鈕。

這產生一個佈局,其中標題正確顯示,中心文本視圖覆蓋所有剩餘的可用空間,並且按鈕不會出現。爲什麼是這樣?中心文本視圖不應該只考慮底部按鈕大小來計算其大小?。

回答

3

在您的中心TextView中使用layout_weight =「1」。

+0

你介意解釋爲什麼這是有效的嗎?我一直在堆棧溢出閱讀有關layout_weight,但無法得到您的解決方案的原因 – Notbad

1

永遠記住拇指規則

如果使用線性佈局,垂直方向儘快找到控制 與

android:layout_height="match_parent"

佈局會忽略所有目前它下面的控制。

希望這有助於

VIPUL

1

代替這個

android:layout_width="match_parent" 
android:layout_height="match_parent" 

使用 'WRAP_CONTENT' 像這樣的

android:layout_width="wrap_content"/"fill_parent" 
    android:layout_height="wrap_content"/"fill_parent" 
+0

這對我不起作用。我在這一行遇到了一個錯誤,我想是因爲「/」字符。 – Notbad

+0

不要同時使用任何一個這.... – NagarjunaReddy

1

應該不是中心文本視圖只根據底部按鈕計算其大小大小?

不,因爲你告訴第二TextView,以配合其母公司的高度,從而FILL_PARENT,因此它會填滿所有的剩餘空間,留下沒有最後TextView

(...)和一個按鈕,放置在底部,layout_gravity =「bottom」。

不幸的是,這不是LinearLayout的工作原理。如果將方向設置爲垂直,基本上只有leftright重力會起作用。反之亦然,使用(默認)水平方向,只有topbottom工作。方向決定View孩子按順序動態定位的方向,這意味着您不能在該方向手動更改「位置」。

現在,爲了獲得預期的效果,你可以給第二TextView0dp的高度和1的權重,導致其動態佔滿全部剩餘空間沒有推動第三TextView斷底。或者,您可以使用RelativeLayout,其中可以直接設置位置,並簡單地指示中間的TextView位於第一個以下,但高於最後一個。