2014-01-14 27 views
3

我想製作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> 

回答

3

使用GridView代替TableLayout。以便您可以調整行和列的寬度和高度。

+0

是否可以讓它們自適應,這樣盒子的大小會根據用戶的屏幕而改變? –

+0

Gridview有GridView的高度和寬度,你可以將它們設置爲wrap_content – mhdtouban

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> 

讓我知道如果它的工作,我沒有您的圖像加載。如果它不起作用,或發佈屏幕截圖,其他人可能會知道該解決方案。