Android GridLayout(not GridView) - 单元格之间的空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21455495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GridLayout(not GridView) - Spaces between the cells
提问by Vamsi Challa
I am using GridLayout
(support) for displaying ImageView
s in my application. There are 3 columns and 5 rows. The problem is that the cells in the GridLayout
automatically get some space between them. I am not setting any padding or margin for the cells. Please refer to the image below. All cells are added dynamically and here is how I add these cells.
我正在使用GridLayout
(支持)ImageView
在我的应用程序中显示s。有 3 列和 5 行。问题是单元格中的单元格GridLayout
之间会自动获得一些空间。我没有为单元格设置任何填充或边距。请参考下图。所有单元格都是动态添加的,这是我添加这些单元格的方法。
Getting Screen Width and Height:
获取屏幕宽度和高度:
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
screenWidth = size.x;
screenHeight = size.y;
rowHeight = (int) (screenHeight * 0.2);
Adding View to GridLayout:
将视图添加到 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 Layout:
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>
Current result:
当前结果:
The red lines show the spaces in between the cells. Also, there is some space on the left side of GridLayout. I have only given 2dp
as layout_margin
. Any reasons why this padding occurs?
红线显示单元格之间的空间。此外,GridLayout 的左侧还有一些空间。我只给出了2dp
作为layout_margin
. 出现这种填充的任何原因?
[EDIT]
[编辑]
Making the following changes removed the spacings.
进行以下更改删除了间距。
gridLayout = (GridLayout) findViewById(R.id.gridlayout_main);
gridLayout.setUseDefaultMargins(false);
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setRowOrderPreserved(false);
Refer to the image below.
请参考下图。
回答by Vamsi Challa
Found the solution.
找到了解决办法。
Making the following changes removed the spacings.
进行以下更改删除了间距。
gridLayout = (GridLayout) findViewById(R.id.gridlayout_main);
gridLayout.setUseDefaultMargins(false);
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setRowOrderPreserved(false);
Refer to the image below.
请参考下图。