tablelayout中行之间的差距

时间:2014-01-14 19:40:26

标签: android android-tablelayout

我想制作6x6平铺游戏。 所以我用6x6 imageView做了一个布局,认为这很容易。 但不,我在行之间有空格,我已经尝试了一段时间让它们离开,但似乎没有任何工作。

这是我的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="0dip"
    >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="30sp"
        android:text="@string/game_text" >
    </TextView>

    <TableLayout
        android:id="@+id/TableLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dip"
         >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="160dp"
            android:layout_height="160dp">

            <ImageView
                android:id="@+id/row1col1"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row1col2"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row1col3"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row1col4"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row1col5"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row1col6"
                android:layout_weight="0.16" >
            </ImageView>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/row2col1"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row2col2"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row2col3"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row2col4"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row2col5"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row2col6"
                android:layout_weight="0.16" >
            </ImageView>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:padding="0dip">

            <ImageView
                android:id="@+id/row3col1"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row3col2"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row3col3"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row3col4"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row3col5"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row3col6"
                android:layout_weight="0.16" >
            </ImageView>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/row4col1"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row4col2"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row4col3"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row4col4"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row4col5"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row4col6"
                android:layout_weight="0.16" >
            </ImageView>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/row5col1"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row5col2"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row5col3"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row5col4"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row5col5"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row5col6"
                android:layout_weight="0.16" >
            </ImageView>
        </TableRow>
        <TableRow
            android:id="@+id/tableRow6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/row6col1"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row6col2"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row6col3"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row6col4"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row6col5"
                android:layout_weight="0.16" >
            </ImageView>

            <ImageView
                android:id="@+id/row6col6"
                android:layout_weight="0.16" >
            </ImageView>
        </TableRow>

    </TableLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:3)

使用GridView而不是TableLayout。这样您就可以调整行和列的宽度和高度。

答案 1 :(得分:1)

我认为你的问题是layout_weight。布局的默认layout_weight是1,这通常很好,因为您可以使用除以该数字。但是,在您的情况下,每行有6个项目。这意味着你必须将1除以6并得到数字0.166666666_,你只需要0.16。

你可以做些什么来解决这个问题,给布局一个layout_weight为6,每个图像视图一个layout_weight为1. 试试这个。我刚刚添加了android:layout_weight =“6”到线性布局并将所有android:layout_weight =“0.16”更改为android:layout_weight =“1”。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="0dip"
    android:layout_weight="6"
    >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="30sp"
        android:text="@string/game_text" >
    </TextView>

    <TableLayout
        android:id="@+id/TableLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dip"
         >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="160dp"
            android:layout_height="160dp">

            <ImageView
                android:id="@+id/row1col1"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row1col2"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row1col3"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row1col4"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row1col5"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row1col6"
                android:layout_weight="1" >
            </ImageView>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/row2col1"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row2col2"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row2col3"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row2col4"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row2col5"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row2col6"
                android:layout_weight="1" >
            </ImageView>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:padding="0dip">

            <ImageView
                android:id="@+id/row3col1"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row3col2"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row3col3"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row3col4"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row3col5"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row3col6"
                android:layout_weight="1" >
            </ImageView>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/row4col1"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row4col2"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row4col3"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row4col4"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row4col5"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row4col6"
                android:layout_weight="1" >
            </ImageView>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/row5col1"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row5col2"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row5col3"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row5col4"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row5col5"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row5col6"
                android:layout_weight="1" >
            </ImageView>
        </TableRow>
        <TableRow
            android:id="@+id/tableRow6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/row6col1"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row6col2"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row6col3"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row6col4"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row6col5"
                android:layout_weight="1" >
            </ImageView>

            <ImageView
                android:id="@+id/row6col6"
                android:layout_weight="1" >
            </ImageView>
        </TableRow>

    </TableLayout>

</LinearLayout>

如果有效,请告诉我,我没有加载您的图片。如果它不起作用,或发布截图,其他人可能知道解决方案。