【问题标题】:Android TableLayout in LinearLayout - Remove spaces in between columns and rowsLinearLayout中的Android TableLayout - 删除列和行之间的空格
【发布时间】:2014-03-24 07:36:08
【问题描述】:

我在 Android 中的 UI 设计比实际制作应用程序更苦恼,所以我尝试了一下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TableRow 
            android:id="@+id/rowOne"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

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

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

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

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

        </TableRow>

        <TableRow 
            android:id="@+id/rowTwo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

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

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

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

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

        </TableRow>

        <TableRow 
            android:id="@+id/rowThree"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

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

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

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

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

        </TableRow>
    </TableLayout>

</LinearLayout>

我一直在尝试删除行和列之间的空格,我尝试为行和按钮设置负边距,如下所示:

android:layout_marginRight="-20sp"

但这真的很“繁琐”,不是很准确,我认为它不会在所有屏幕上缩放相同(如果我错了,请纠正我)。我还通过赋予行 layout_weight 属性来使它们覆盖屏幕的整个宽度(我知道权重只是赋予元素的重要性)。也许我需要使用stretchColumns?或者也许还有另一个标签在这里可能有用?

我将它放在 LinearLayout 中的原因是因为我计划添加其他元素并尝试使用这些元素。

【问题讨论】:

  • “空格”是Android默认背景为Button造成的;有内部填充。您可以通过将所有android:background 更改为任何颜色(例如#ccc)来测试它,并查看空格如何消失。
  • @AndrewT。谢谢!!我试过用按钮填充做事情,但我不知道这是由默认背景引起的!

标签: android android-layout android-linearlayout android-tablelayout


【解决方案1】:

更换你的按钮:

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

<Button 
    android:id="@+id/BFour"
    android:layout_height="fill_parent"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:text="Button" />

【讨论】: