【问题标题】:GridLayout(not GridView) - Spaces between the cellsGridLayout(not GridView) - 单元格之间的空间
【发布时间】:2014-02-22 16:43:52
【问题描述】:

我正在使用GridLayout(support) 在我的应用程序中显示ImageViews。有 3 列和 5 行。问题是GridLayout 中的单元格会自动在它们之间获得一些空间。我没有为单元格设置任何填充或边距。请参考下图。所有单元格都是动态添加的,下面是我添加这些单元格的方法。

获取屏幕宽度和高度:

Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
screenWidth = size.x;
screenHeight = size.y;
rowHeight = (int) (screenHeight * 0.2);

将视图添加到 GridLayout:

    GridLayout.LayoutParams params = new GridLayout.LayoutParams(
            getSpec(rowColumn[0]), getSpec(rowColumn[1]));

    params.height = rowHeight;

    if (rowColumn[1].equalsIgnoreCase("col_full")) {
        params.width = (int) (screenWidth);
    } else if (rowColumn[1].equalsIgnoreCase("col_two_part")) {
        params.width = (int) (screenWidth * 0.6);
    } else {
        params.width = (int) (screenWidth * 0.3);
    }

    ImageButton image = (ImageButton) imageHashMap
            .get(reOrderedButtonsListCopy.get(i));
    image.setLayoutParams(params);

    gridLayout.addView(image, params);

XML 布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.xx.xxx"
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <android.support.v7.widget.GridLayout
            xmlns:app="http://schemas.android.com/apk/res/com.xx.xxx"
            android:id="@+id/gridlayout_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            app:columnCount="3"
            app:rowCount="5" >
        </android.support.v7.widget.GridLayout>

    </LinearLayout>

</ScrollView>

当前结果:

红线表示单元格之间的空格。此外,GridLayout 的左侧还有一些空间。我只给了2dplayout_margin。出现这种填充的任何原因?

[编辑]

进行以下更改删除了间距。

gridLayout = (GridLayout) findViewById(R.id.gridlayout_main);
gridLayout.setUseDefaultMargins(false);
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setRowOrderPreserved(false);

参考下图。

【问题讨论】:

  • 为什么不能使用固定的 xml 在 gridView 中显示单个单元格?
  • 按钮的位置会根据用户活动而变化,因此我无法拥有固定的 xml。
  • OK.. 现在增加这两个值 app:columnCount="3" app:rowCount="5"。您可以观察网格布局的变化
  • 我的要求是我有一个 3 列 5 行的 GridLayout。你能解释一下,更改这些值将如何删除列和行之间的空格?

标签: android android-gridlayout


【解决方案1】:

找到了解决办法。

进行以下更改删除了间距。

gridLayout = (GridLayout) findViewById(R.id.gridlayout_main);
gridLayout.setUseDefaultMargins(false);
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setRowOrderPreserved(false);

参考下图。

【讨论】:

  • 这是非常好的信息,但我还想指出,android:useDefaultMargins 可以在 xml 中设置为 TRUE 或 FALSE,或者通过提供 marginTop/Bottom/Left/Right 来覆盖。我个人喜欢统一性,所以我在 style.xml 中创建了一个
  • 您找到的解决方案是正确的,它确实帮助了我。但是@G_V 的建议也可能会有所帮助:用它的评论来编辑答案会很好:)
  • 感谢您将我指向 useDefaultMarginsalignmentMode。正是我想要在项目之间添加一些间距的东西。