Reputation: 818
I want to make a 6x6 tile-game. So I made a layout with 6x6 imageView, thought that would be easy enough. But no, I got spaces between rows and I've been trying for some time now to get them away but nothing seems to work.
Here is my 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>
Upvotes: 3
Views: 4830
Reputation: 721
I think your problem is the layout_weight. The default layout_weight for a layout is 1 which is normally fine because you can use divide that number. In your case, however, you have 6 items per row. That means you have to divide 1 by 6 and that gives you the number 0.166666666_ and you simply have 0.16.
What you can do to fix this is give the layout a layout_weight of 6 and each image view a layout_weight of 1. Try this. I have just added android:layout_weight="6" to the Linear Layout and changed all the android:layout_weight="0.16" to 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>
Let me know if it works, I didn't have your images to load. If it doesn't work, or post a screenshot and someone else may know the solution.
Upvotes: 1
Reputation: 881
Use GridView instead of TableLayout. So that you can adjust the width and height of rows and columns.
Upvotes: 3