【问题标题】:How to remove space in between Android Grid Layout如何删除Android Grid Layout之间的空间
【发布时间】:2017-02-22 02:02:41
【问题描述】:

正如标题所述,我只想在网格布局中的列/行之间有一个几乎没有空间的布局。请参考下图作为示例。我只是想让所有的按钮占据它们所在单元格的所有空间,并且相邻的单元格可以相互对齐。这将允许更多的瓷砖外观。

我看过这个问题: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:horizontalSpacing="0dip" android:verticalSpacing="0dip"吗?

标签: java android user-interface


【解决方案1】:

按钮有一个默认的填充。你可以设置一个背景来改变它。

    <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>

【讨论】: