2017-02-22 121 views
0

正如標題所述,我只想在網格佈局中的列/行之間使用盡可能少的空間進行佈局。作爲例子參考下面的圖片。我只想讓所有的按鈕佔據它們所在的單元格的所有空間,並且相鄰的單元格互相對齊。這將允許更多的瓷磚外觀。如何刪除Android網格佈局之間的空間

我已經看過了這個問題:GridLayout (not GridView) how to stretch all children evenly - 除了這一點:GridLayout(not GridView) - Spaces between the cells

但它不回答或解決我的問題。非常感謝您的幫助和建議。

這是我的gridLayout的XML代碼。

GridLayout 
    android:id="@+id/grid" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#32ffffff" 
    android:rowOrderPreserved="false" 
    android:columnCount="3" 
    android:padding="10dp" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true"> 

    <Button 
     android:id="@+id/hello" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_row="0" 
     android:text="Hello" 
     android:layout_columnWeight="1" 
     android:layout_columnSpan="2" 
     android:layout_gravity="fill" 
     android:padding="5dp"/> 

    <Button 
     android:id="@+id/sorry" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_row="1" 
     android:layout_gravity="fill" 
     android:text="Sorry" /> 

    <Button 
     android:id="@+id/thank" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:layout_row="1" 
     android:layout_gravity="fill" 
     android:text="Thank You" /> 

    <Button 
     android:id="@+id/myname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="My \nName \nIS" 
     android:layout_column="2" 
     android:layout_row="0" 
     android:layout_rowSpan="2" 
     android:layout_rowWeight="1" /> 

    <Button 
     android:id="@+id/howto" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="How To Say ..?" 
     android:layout_columnSpan="2" 
     android:layout_column="0" 
     android:layout_row="2" 
     android:layout_columnWeight="1" 
     android:layout_gravity="fill"/> 

    <Button 
     android:id="@+id/welcome" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="2" 
     android:layout_row="2" 
     android:text="You're\nWelcome" /> 

    <Button 
     android:id="@+id/yourname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Your Name?" 
     android:layout_column="0" 
     android:layout_row="3" 
     android:layout_gravity="fill" 
     /> 

    <Button 
     android:id="@+id/howareyou" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="How are you?" 
     android:layout_columnSpan="2" 
     android:layout_column="1" 
     android:layout_row="3" 
     android:layout_columnWeight="1" 
     android:layout_gravity="fill"/> 

    <Button 
     android:id="@+id/english" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Do you speak english?" 
     android:layout_columnSpan="3" 
     android:layout_column="0" 
     android:layout_row="4" 
     android:layout_columnWeight="1" 
     android:layout_gravity="fill"/> 

</GridLayout> 

Android layout at the moment

+0

你嘗試'機器人:horizo​​ntalSpacing = 「0dip」 機器人:verticalSpacing = 「0dip」'? –

回答

0

的按鈕有一個默認的padding.You可以設置背景來改變它。

<GridLayout 
    android:id="@+id/grid" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:background="#32ffffff" 
    android:columnCount="3" 
    android:padding="10dp" 
    android:rowOrderPreserved="false"> 

    <Button 
     android:id="@+id/hello" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_columnSpan="2" 
     android:layout_columnWeight="1" 
     android:layout_gravity="fill" 
     android:layout_row="0" 
     android:background="#ffffff" 
     android:padding="5dp" 
     android:text="Hello" /> 

    <Button 
     android:id="@+id/sorry" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_gravity="fill" 
     android:layout_row="1" 
     android:background="#ffffff" 
     android:text="Sorry" /> 

    <Button 
     android:id="@+id/thank" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:layout_gravity="fill" 
     android:layout_row="1" 
     android:background="#ffffff" 
     android:text="Thank You" /> 

    <Button 
     android:id="@+id/myname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="2" 
     android:layout_row="0" 
     android:layout_rowSpan="2" 
     android:layout_rowWeight="1" 
     android:background="#ffffff" 
     android:text="My \nName \nIS" /> 

    <Button 
     android:id="@+id/howto" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_columnSpan="2" 
     android:layout_columnWeight="1" 
     android:layout_gravity="fill" 
     android:layout_row="2" 
     android:background="#ffffff" 
     android:text="How To Say ..?" /> 

    <Button 
     android:id="@+id/welcome" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="2" 
     android:layout_row="2" 
     android:background="#ffffff" 
     android:text="You're\nWelcome" /> 

    <Button 
     android:id="@+id/yourname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_gravity="fill" 
     android:layout_row="3" 
     android:background="#ffffff" 
     android:text="Your Name?" /> 

    <Button 
     android:id="@+id/howareyou" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:layout_columnSpan="2" 
     android:layout_columnWeight="1" 
     android:layout_gravity="fill" 
     android:layout_row="3" 
     android:background="#ffffff" 
     android:text="How are you?" /> 

    <Button 
     android:id="@+id/english" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_columnSpan="3" 
     android:layout_columnWeight="1" 
     android:layout_gravity="fill" 
     android:layout_row="4" 
     android:background="#ffffff" 
     android:text="Do you speak english?" /> 

</GridLayout> 
+0

哦,太棒了!不知道這個,非常感謝你! – ZarifS