如何删除Android网格布局之间的空间

问题描述:

正如标题所述,我只想在网格布局中的列/行之间使用尽可能少的空间进行布局。作为例子参考下面的图片。我只想让所有的按钮占据它们所在的单元格的所有空间,并且相邻的单元格互相对齐。这将允许更多的瓷砖外观。如何删除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”'? –

的按钮有一个默认的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