【问题标题】:Center the content of a GridView将 GridView 的内容居中
【发布时间】:2013-05-03 01:03:02
【问题描述】:

我的GridView 本身是居中的,但它的内容不在其中居中。这是一个屏幕,底部和右侧的浅蓝色是我为GridView设置的背景色。

您可以看到内容被推到顶部和左侧。我想要内容,我的游戏板,正好位于 GridView 的中间,浅蓝色背景颜色均匀地环绕四周。

这是我的 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textFieldFU"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<GridView
    android:id="@+id/gridview"
    android:layout_marginTop="0dp"
    android:layout_marginLeft="45dp"
    android:layout_marginRight="45dp"
    android:layout_width="fill_parent"
    android:layout_height="485dp"
    android:gravity="center"
    android:horizontalSpacing="0dp"
    android:numColumns="8"
    android:background="@android:color/holo_blue_bright"
    android:verticalSpacing="0dp" />

如果有帮助,我的 getView 在我的 ImageAdapter 课程中:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView iv;
    if (convertView != null) {
        iv = (ImageView) convertView;
    } else {
        iv = new ImageView(context);
        iv.setLayoutParams(new GridView.LayoutParams(60, 60));
        iv.setScaleType(ScaleType.FIT_CENTER);
        iv.setPadding(0, 0, 0, 0);
        if(position < 8 && position % 2 ==0){
            iv.setBackgroundColor(Color.DKGRAY);
        }
        else if(position > 7 && position < 16 && position % 2 ==1){
            iv.setBackgroundColor(Color.DKGRAY);
        }
        else if(position > 15 && position < 24 && position % 2 ==0){
            iv.setBackgroundColor(Color.DKGRAY);
        }
        else if(position > 23 && position < 32 && position % 2 ==1){
            iv.setBackgroundColor(Color.DKGRAY);
        }
        else if(position > 31 && position < 40 && position % 2 ==0){
            iv.setBackgroundColor(Color.DKGRAY);
        }
        else if(position > 39 && position < 48 && position % 2 ==1){
            iv.setBackgroundColor(Color.DKGRAY);
        }
        else if(position > 47 && position < 56 && position % 2 ==0){
            iv.setBackgroundColor(Color.DKGRAY);
        }
        else if(position > 55 && position < 64 && position % 2 ==1){
            iv.setBackgroundColor(Color.DKGRAY);
        }
        else
            iv.setBackgroundColor(Color.GRAY);
    }
    iv.setImageResource(images[position]);
    return iv;
}

【问题讨论】:

标签: java android android-gridview


【解决方案1】:

尝试使用these options 之一在视图周围创建边框。我会避免任何任意的固定值,例如 45dp 边距和 485dp 高度,因为它在其他屏幕尺寸上肯定看起来不正确。

另外,我建议您使用GridLayout 而不是 GridView。从技术上讲,它需要 API 级别 14,但您可以使用支持库中的 API 级别来支持旧版本的 Android。这不会是一个微不足道的更改,因为您需要通过调用 addView 而不是覆盖 getView 方法来添加磁贴。

GridLayout 更适合绘制具有固定数量且始终显示的图块的网格。 GridView 是一个更复杂的小部件,它具有大量用于延迟加载图块的内部逻辑,您在这里当然不需要。它也可能使布局更容易。

【讨论】:

  • 好的,我使用了 45dp,因为我无法让 android 在我的列之间添加的填充消失,除非将 android:horizo​​ntalSpacing 设置为一个大的负数,这似乎同样是一种不好的做法.摆弄后忘记删除高度 485dp。
  • 我现在必须去上班,但我会尝试这些选项或采纳您的 GridLayout 建议,即使我很懒惰。之后回来检查答案。
  • 是的,我知道将其移至GridLayout 需要做大量的工作,但我发现它使用起来更加直观。在 XML 中,您只需指定 android:columnCount。然后,您可以直接在 XML 中将 ImageView 切片添加为 GridLayout 的子项,或者通过调用 gridView.addView(iv, 60, 60); 之类的方法将它们添加到您的代码中。同样,如果您希望它在 ICS 之前的设备上工作,您需要使用支持库。
【解决方案2】:

在网格视图描述中添加这一行:

android:layout_centerInParent="true"

【讨论】:

  • 我之前尝试过,它只是将整个 GV 置于屏幕中间。背景颜色仍然只出现在右侧和底部,仍然没有使内容居中?
  • 也许我应该把 GridView 放在别的东西里面,然后给它应用一个 bg 颜色?您知道如何将 gridView 放入其他容器中吗?
猜你喜欢
  • 2020-10-19
  • 2015-06-10
  • 2023-04-05
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 2019-12-22
相关资源
最近更新 更多