2011-06-07 24 views
0

此代碼有什麼問題?爲什麼按鈕不在這個表格列中?Button並不想在TableLayout中正確使用

<TableLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      > 
     <TableRow> 
      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/str1" 
        /> 
      <EditText 
        android:id="@+id/id1" 
        android:layout_width="70dp" 
        android:layout_height="wrap_content" 
        /> 
      <Button 
        android:id="@+id/id2" 
        android:layout_width="wrap_content" 
        android:layout_height="65dp" 
        android:text="@string/str2" 
        android:gravity="right"/> 
     </TableRow> 
</TableLayout> 
+0

它去哪裏? – 2011-06-07 17:51:02

+0

它被粘貼到EditText(右側)。 – sandalone 2011-06-07 21:00:17

回答

4

您應該使用機器人:stretchColumns =在TableLayout「1」,讓你的EditText推你的按鈕向右。

或者您可以使用android:layout_weight屬性對錶中的視圖行相應地分配空間。

另外,您不需要在TableRow中的視圖上指定寬度和高度屬性。

<TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:stretchColumns="1"> 
     <TableRow> 
      <TextView 
       android:text="@string/str1" 
       /> 
      <EditText 
       android:id="@+id/id1" 
       /> 
      <Button 
       android:id="@+id/id2" 
       android:text="@string/str2" 
       /> 
     </TableRow> 
    </TableLayout> 
+0

'android:stretchColumns =「1」'missing。謝謝 – sandalone 2011-06-08 17:38:32

3

android:gravity="right"用於Button內的文本。由於它的寬度設置爲wrap_content,所以它完全沒有效果。

您需要改用android:layout_gravity。請參閱:http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_gravity

標準重力常數,小孩可以提供給其父母。定義如何將視圖(包括其x軸和y軸)放置在其父視圖組中。

必須是一個或多個(用'|'分隔)以下常量值。

(...)

右爲0x05推對象到其容器的權利,而不是改變它的大小。

+0

問題是我已經嘗試了兩個重力attibutes,都沒有給我結果。我認爲較低的帖子是正確的:我忘了屬性'stretchColumns'。 – sandalone 2011-06-07 21:12:24

+1

這就是羅馬尼亞人說的(它可能對某人有幫助):'重力意味着「將重力應用於這個視圖的內容」,而layout_gravity意味着「在其父項中對這個視圖應用重力。」因此,在TextView中,重力會對齊TextView的 範圍內的文本,而layout_gravity會將TextView與其父級範圍內的對齊。' – sandalone 2011-06-07 21:13:16

相關問題