gpt4 book ai didi

android - GridLayout(不是 GridView)- 单元格之间的空间

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:01:12 26 4
gpt4 key购买 nike

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

获取屏幕宽度和高度:

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

将 View 添加到 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>

当前结果:

enter image description here

红线表示单元格之间的空间。此外,GridLayout 的左侧有一些空间。我只给 2dp 作为 layout_margin。发生这种填充的任何原因?

[编辑]

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

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

请引用下图。

enter image description here

最佳答案

找到解决方案。

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

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

请引用下图。

enter image description here

关于android - GridLayout(不是 GridView)- 单元格之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21455495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com