6

我有一个基于 GridView 的日历。android:listSelector="@null"根据我从该站点获得的建议,我有以下 XML 布局,其中选择器设置为 null 。现在我在 GridView 的右侧得到了几个像素宽的条带。为什么?我已尽我所能,但无济于事。这是我的 XML 布局:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <GridView
                android:id="@+id/calendar"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:horizontalSpacing="-1px"
                android:numColumns="7"
                android:stretchMode="columnWidth" 
                android:gravity="center"
                android:verticalSpacing="-1px"
                android:listSelector="@null" >

            </GridView>

        </LinearLayout>

我得到的是这张照片:

在此处输入图像描述

4

5 回答 5

6

该空间是由于网格每一行的计算不完善造成的。例如,您的设备宽度是 320 像素,并且您有 7 行,尝试任何符合 320 像素的计算。如果每个单元格的宽度为 45.71428571428571 px,则只能缩小。

其他选项

在网格中应用 android:gravity="center" 属性,以便空间从左到右均分

于 2012-05-23T14:07:28.450 回答
4

就我而言,我的水平间距为 1dp

android:horizontalSpacing="1dp"

所以要解决这个问题,我只需将正确的填充设置为 -1dp

android:paddingRight="-1dp"

即使我希望因此在左侧有一个空间,但它工作正常

于 2014-08-22T18:21:48.183 回答
0

尝试使用

android:layout_margin="0dp"
android:padding="0dp"

您也可能为滚动条分配了该空间。
为什么你的垂直和水平间距为负值?

于 2012-05-23T13:52:33.400 回答
0

我遇到了同样的问题,但在我的情况下,我无法控制GridView实例,所以我无法读取列宽。不过,我可以将其容器子类化。
这不是很正统,但您可以覆盖该onMeasure方法并添加一些像素。
像这样的东西:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int RIGHT_MARGIN_OVERSIZE_DIP = 6;
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = this.getMeasuredWidth()
    int overSize = (int)((getResources().getDisplayMetrics().density*RIGHT_MARGIN_OVERSIZE_DIP) + 0.5f);
    widthMeasureSpec = MeasureSpec.makeMeasureSpec(width + overSize, MeasureSpec.getMode(widthMeasureSpec));        
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
于 2013-09-19T19:45:35.680 回答
0
grid.setColumnWidth(grid.getColumnWidth()+1);

这将获得当前的 ColumnWidth 并向其添加 1 个像素。记得在 onCreateView 之后使用它,因为在此之前需要初始化网格。

此外,您的网格视图应使用 match_parent(高度和宽度)。列中的图像/布局也应为 match_parent。

于 2015-04-01T13:37:41.963 回答