【问题标题】:Android GridView imperfection, how to remove extra white space to the rightAndroid GridView 不完善,如何去掉右边多余的空白
【发布时间】:2012-05-23 13:37:22
【问题描述】:

我有一个基于 GridView 的日历。根据我从该站点获得的建议,我有以下 XML 布局,其中选择器设置为 null 因此 android:listSelector="@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>

我得到的是这张照片:

【问题讨论】:

  • 可能是滚动条问题?尝试将滚动条可见性设置为 false。
  • 您是否尝试过将水平间距从 -1 更改为 0?
  • @Demonick,我尝试了滚动条问题,但没有成功。请参阅下面的dinesh sharma's 答案似乎有道理。
  • 您可以尝试在主布局中采用一种布局,并将您的 gridView 放在此布局中,并在此布局中应用 android:gravity="center_horizo​​ntal" 属性,这可能会帮助您和另外一件事将 gridView 的 layout_width 更改为“包装内容”

标签: android gridview selector margins


【解决方案1】:

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

其他选项

申请 android:gravity="center" 网格中的属性,以便空间从左到右平均分配

【讨论】:

  • 是的,你是对的@dinesh-sharma。在横向模式下,间隙几乎消失,证明问题与屏幕尺寸有关。不过,我还没有让android:gravity="center" 工作。然而,这看起来相当微不足道。谢谢!
  • 我尝试了android:gravity="center"android:layout_gravity="center" 解决方案,但它们没有奏效。我有一个折衷的解决方案依赖于在代码中指定列宽,以使它们的组合宽度大于设备的屏幕宽度。这会强制列填满屏幕,但最后一列会被略微剪裁 - 如果您有边框,则不是很好,因为边框消失了。因此指定了 with:[link]bytecapsule.blogspot.com/2011/08/… (ByteCapsule)
【解决方案2】:

在我的情况下,我的水平间距为 1dp

android:horizontalSpacing="1dp"

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

android:paddingRight="-1dp"

尽管我希望因此而在左侧获得一个空间,但它工作正常

【讨论】:

  • 我有 Horizo​​ntalSpacing="0dp" 无论如何这个解决方案对我有帮助。空白空间消失了。
  • @Rempelos 如果我没记错的话,paddingRight 中的数字应该是-horizo​​ntalSpacing,所以如果你的horizo​​ntalSpacing 是2dp 那么你的paddingRight 应该是-2
  • @JiMMaR 实际上你是对的。我将这个案例与我的案例混淆了,我有 horizontalSpacing="0dp" 并且额外的空间仍然出现在右侧,但只添加 paddingRight="-1dp" 修复了它。
【解决方案3】:

尝试使用

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

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

【讨论】:

    【解决方案4】:

    我遇到了同样的问题,但我无法控制 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);
    }
    

    【讨论】:

      【解决方案5】:
      grid.setColumnWidth(grid.getColumnWidth()+1);
      

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

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

      【讨论】:

        猜你喜欢
        • 2012-01-18
        • 2018-09-13
        • 2013-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-23
        • 2014-08-05
        • 2022-01-21
        相关资源
        最近更新 更多