2012-03-05 104 views
0

我有點困惑,我目前正在設計一個活動的用戶界面,並在上述用戶界面中的兩個TextView小工具有點奇怪。Android - 奇怪的TextView行爲

我已將背景設置爲android:background="#CCCCCC"以便更輕鬆地觀察行爲,當我獲得我期望的外觀時,最終會將其刪除。

當兩個TextView是空的,什麼行爲是我從我的UI期待:

enter image description here

但當TextView都充滿了不同長度的字符串,寬度得到改變:

enter image description here

下面是附加到我的UI行的代碼。

 <TableRow android:weightSum="2" > 
      <TextView 
       android:id="@+id/tvDate" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#CCCCCC" 
       android:gravity="center_vertical|center_horizontal" 
       android:padding="4dp" 
       android:textColor="#000000" /> 

      <View 
       android:layout_width="8dp" 
       android:layout_height="fill_parent" /> 

      <TextView 
       android:id="@+id/tvTime" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#CCCCCC" 
       android:gravity="center_vertical|center_horizontal" 
       android:padding="4dp" 
       android:textColor="#000000" /> 
     </TableRow> 

到目前爲止,我已經嘗試到android:layout_width參數不同android:paddingandroid:layout_weight組合改變爲值過多。但沒有任何東西給我預期的效果(這是爲了讓兩個TextView佔據相同的空間量)。

我遠離Android XML專家,所以任何指針將不勝感激。

+0

我認爲還有android:weight ...檢查文檔。這應該解決它。 – trgraglia 2012-03-05 15:41:36

+0

我目前使用'android:layout_weight'你甚至在評論之前檢查我的問題是否麻煩? – 2012-03-05 15:44:53

+0

如果它只是兩排,我會建議使用2個水平線性佈局,與同等重量的孩子。 – 2012-03-05 15:49:39

回答

2

我會建議使用LinearLayout。

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="1.0" 
    android:orientation="horizontal"> 
<TextView 
    android:id="@+id/tvDate" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:background="#CCCCCC" 
    android:gravity="center_vertical|center_horizontal" 
    android:paddingRight="4dp" 
    android:textColor="#000000" /> 
<TextView 
    android:id="@+id/tvTime" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:background="#CCCCCC" 
    android:gravity="center_vertical|center_horizontal" 
    android:paddingLeft="4dp" 
    android:textColor="#000000" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="1.0" 
    android:orientation="horizontal"> 
<Button 
    android:id="@+id/buttonDate" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:gravity="center_vertical|center_horizontal" 
    android:paddingRight="4dp" 
    android:text="Pick Date" /> 
<Button 
    android:id="@+id/buttonTime" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:gravity="center_vertical|center_horizontal" 
    android:paddingLeft="4dp" 
    android:text="Pick Time" /> 
</LinearLayout> 
+0

這樣做,謝謝你的幫助! – 2012-03-05 16:05:17