2014-09-02 140 views
1

我想要做的是從TableLayout中刪除每個填充和邊距,並顯示兒童按鈕平鋪。Android TableLayout刪除填充

這是activity_main.xml代碼

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".Main" 
    android:orientation="horizontal" 
    android:baselineAligned="false"> 

    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

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

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button" 
      android:id="@+id/button4" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button" 
      android:id="@+id/button2" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button" 
      android:id="@+id/button3" /> 
    </TableRow> 

</TableLayout> 

所有的補白設置爲0

這就是我得到:

這是我什麼試圖得到:

+0

您使用FILL_PARENT你應該是您使用的API使用WRAP_CONTENT – geekCode 2014-09-02 22:58:37

+0

? – 2014-09-02 23:03:11

+0

您是對的,但是我使用API​​級別14 – bukk530 2014-09-02 23:53:03

回答

1

TableLayoutTableRow都是LinearLayouts。因此,使用weight屬性會給你想要的結果:

  1. 設置TableLayout's寬度和高度match_parent
  2. TableRows設置layout_height="0dp"layout_weight="0.5",使他們適應每個屏幕高度的50%;
  3. 將每個Button's高度設置爲match_parent以填充該行的高度,並且還設置其layout_width="0dp"layout_weight="0.5"以平等地適合該行的寬度。

這裏的佈局:

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

<TableRow 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight="0.5"> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.5" 
     android:text="New Button" 
     android:id="@+id/button1" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.5" 
     android:text="New Button" 
     android:id="@+id/button4" /> 
</TableRow> 

<TableRow 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight="0.5"> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.5" 
     android:text="New Button" 
     android:id="@+id/button2" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.5" 
     android:text="New Button" 
     android:id="@+id/button3" /> 
</TableRow> 

</TableLayout> 

這裏是它的樣子:

enter image description here

+0

我複製了你的例子。這是行不通的。按鈕有填充 – Butsaty 2015-12-05 10:07:58

+0

@Butsaty沒有看到你精確的佈局xml很難說什麼「不起作用」。這種佈局有助於問題所有者......提出一個新問題,如果你願意的話,讓我知道, - 我會看看這個問題。 – Onik 2015-12-05 10:15:29

+0

@Butsaty當你冷靜下來,請解釋一個答案到底出了什麼問題。否則,這可能意味着你是以錯誤的方式使用了答案。例如,像這裏一樣,根本沒有填充,所以它必須是代碼中的其他東西。 – Onik 2015-12-05 10:25:58