我正在我的应用程序中实现一个网格视图,我想使用java代码获得网格视图中连续行之间的水平间距和连续列之间的垂直间距。
有可能吗?有没有什么直接的方法来获取间距?
发布于 2013-05-02 23:13:29
您可以在GridView标签中使用android:verticalSpacing和android:horizontalSpacing,并根据需要提供间距。
例如:
<GridView
android:layout_height="wrap_content"
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp" // space between two items (horizontal)
android:verticalSpacing="10dp"> // space between two rows (vertical)
在java try上:
"gridviewname".getHorizontalSpacing()
"gridviewname".getVerticalSpacing()发布于 2015-03-13 06:14:39
您也可以在Java中尝试
mGridView.setHorizontalSpacing(4);
mGridView.setVerticalSpacing(4);或者在XML中
<GridView
android:layout_height="wrap_content"
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="4dp"
android:verticalSpacing="4dp">发布于 2014-08-28 19:08:04
如果您的minSdk是16,那么您可以使用mGridView.getHorizontalSpacing()或mGridView.getVerticalSpacing()。
如果没有,您可以在xml布局android:verticalSpacing="@dimen/grid_spacing"中设置,并在代码中使用getResources().getDimensionPixelSize(R.dimen.grid_spacing)。
这样,您只需在一个位置更改大小,就会同时影响xml布局和代码。
在/res/values/dimens.xml中添加以下行-
<dimen name="grid_spacing">10dp</dimen>https://stackoverflow.com/questions/16341284
复制相似问题