11

我正在使用GridLayout(支持)ImageView在我的应用程序中显示 s。有 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 的左侧还有一些空间。我只给出了2dpas layout_margin。发生这种填充的任何原因?

[编辑]

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

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

请参考下图。

在此处输入图像描述

4

1 回答 1

13

找到了解决方案。

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

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

请参考下图。

在此处输入图像描述

于 2014-01-30T12:20:38.733 回答