2014-09-02 120 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