2011-11-30 49 views
1

不確定從這個問題標題清楚我的意思,所以這裏是細節。類型「<fill_parent><wrap_content>」的佈局

我想要的是兩個TextView控件水平對齊的簡單佈局。第一個TextView可能包含幾行文本,第二個TextView將只包含一個數字。

第二個TextView應該始終具有widht,因爲它需要顯示其內容,右對齊,高度應該等於第一個TextView的高度,以便我可以顯示垂直居中的數據。第一個TextView應該佔用剩餘空間(用於寬度)並根據需要垂直拉伸。

例1:

--------------------------------- 
    |Small Text.     123| 
    --------------------------------- 

例2:

--------------------------------- 
    |Long Text starts here ....  | 
    |... continues here ....... 123| 
    |... and finishes here ....  | 
    --------------------------------- 

的LinearLayout與水平方向,因爲它把控制由左到右是不是在這裏好。在這種情況下,我需要先將wrap_content設置爲第二個TextView,然後將fill_parent設置爲第一個TextView。

玩RelativeLayout我也無法得到描述佈局。

我不能在這裏使用layout_weight方法,因爲我不知道第二個TextView中數據的長度:在一種情況下,它可能是1個字符,在另一個情況下可能是8個字符。所以在1個字符的情況下,我將有未使用的空間。

那麼,我有機會建立這樣的佈局嗎?

+0

您是否嘗試過使用TableLayout這個? –

回答

4

像下面這樣的佈局應該爲你做的伎倆:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:singleLine="false" 
     android:text="TextView" /> 
    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:gravity="center_vertical|right" 
     android:text="TextView" /> 
</LinearLayout> 
+0

是的,它的確如此。謝謝。 –

+0

使用layout_width =「match_parent」來查看使用layout_weight屬性的任何地方。 – Jayabal

-1

這裏對使用minWidth和maxWidth屬性很有用。