【问题标题】:How to center GridView correctly?如何正确居中 GridView?
【发布时间】:2012-07-19 04:37:59
【问题描述】:

我一直在尝试和尝试,但没有这样的运气。我在这里的所有其他答案以及与居中GridView 相关的所有其他答案都很幸运,也没有运气。这是我的GridViewxml:

<GridView
        android:id="@+id/calendar_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="7"
        android:columnWidth="40dp"
        android:verticalSpacing="1dp"
        android:horizontalSpacing="1dp"
        android:paddingTop="2dp"
        android:stretchMode="columnWidth"
        android:layout_gravity="right"
        android:gravity="right"
        android:background="#696969" ></GridView>

我已经尝试了所有我能想到的组合,但效果不大。至少它在设备之间不一致。这是它一直在做的事情的形象。

正如您在 GridView 的右侧看到的,有间距。我试图不断改变这一点,但它几乎永远不会奏效。使用android:stretchMode 确实会改变它,但它会弄乱我在单元格之间的间距,所以这不起作用。如您所见,使用 Gravity 也会失败。我已经为它尝试了所有其他值,但它仍然没有改变。它粘在左边。我还觉得奇怪的是,尝试将 android:columnWidth 的大小从 40 以下的任何值更改它也无济于事。当我将 40 增加到 45 时,它也没有任何变化。但是将其更改为 50 会使右侧的空间消失。但是当它放在我的手机上时,间距就在那里!两周前我跳过了这个以继续处理其他问题,但看起来我还没有解决这个问题,甚至不明白为什么我没有尝试任何工作。

如果它可以帮助任何人在这里帮助我,我还可以提供构成单元格的 xml。但我不确定这是否重要。但这里是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/day_num"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="10dp"
        android:background="#000000"
        android:height="35dp"
        android:gravity="center_vertical|center_horizontal" />


</LinearLayout>

如果有帮助,GridView 的父级是 LinearLayout,其中:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="0"
    android:gravity="center" >

还有一个权重为 1 的兄弟,但我认为这不应该是因为权重为 0 使该部分首先设置其空间。

编辑

所以我一直在玩它,非常恼火,特别是因为似乎没有多少人有这个问题,但看起来不管我把columnWidth 做得多么小,GridView 几乎没有变小。事实上,在我达到 50dp 之前,你看到的同样的差距就是它。但是当我进入纵向模式时,间隙再次出现,所以我将它提高到 90dp(我尝试过,没有改变任何东西),它就消失了。奇怪的是GridView 除了填补空白之外没有改变。所以我可以把它设为 200dp,它在我的屏幕尺寸和任何其他较小的屏幕上看起来都很完美。一旦我使用平板电脑,我怀疑我需要增加更多,但关键是,这是一种奇怪的行为。有人遇到过吗?

【问题讨论】:

  • 是的,我也在我的代码中发现了这个问题,这很奇怪,我还没有找到解决方案......所以你在这方面取得了成功吗??
  • @GOLDEE 似乎没有解决方案。我当然走的是把它拉到两边的路线,效果很好。基本上将其放置在某个视图中并操纵该父视图以控制其在您的视图组中的布局。

标签: android android-gridview


【解决方案1】:

您可能希望将您的GridView 放入RelativeLayout,然后将GridViewlayout_centerInParent 属性设置为true。像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
            android:id="@+id/calendar_grid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="7"
            android:columnWidth="40dp"
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"
            android:paddingTop="2dp"
            android:stretchMode="columnWidth"
            android:background="#696969"
            android:layout_centerInParent="true" />

</RelativeLayout>

RelativeLayout 让您更好地控制孩子的位置。

【讨论】:

  • 嗯,这听起来确实是一个更好的选择。我可能会采取的一种。你读过我的编辑吗?你知道为什么它的行为是这样的吗?我希望 Android 文档更好-_-
  • 过去一段时间我也遇到过类似的问题。我认为问题是由于右侧的剩余空间无法在各列之间平均分配(例如,您有 7 列,仅剩余 6 个像素)。使用 LinearLayout,您对位置没有太多控制,因为它总是从顶部/左侧开始布置子级,因此所有剩余空间都在右侧/底部。
  • 啊,这完全有道理。当我增加 columnWidth 的大小时,我开始假设这一点。但不确定。我想在这些情况下要么避免它,要么像我现在一样使用它。我绝对可以使用 TableLayout。现在有点后悔。无论如何,感谢您的宝贵时间。
  • @Andy 当然,没问题。为子级使用带有centerInParent 的相对布局应该在子级的所有侧面均分剩余空间,从而有效地使您的 GridView 居中。
  • 如果 gridview 的宽度和高度设置为 match_parent,这将如何工作?
【解决方案2】:

这是我的工作配置:

        <GridView
        android:id="@+id/gridView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:cacheColorHint="@color/white"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:padding="5dp"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:gravity="center"
        />

【讨论】:

    【解决方案3】:

    您需要设置TextView的Parent Layout的Gravity或TextView的LayoutGravity,请更改Cell Layout如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:gravity="center_vertical|center_horizontal" >
        <TextView 
            android:id="@+id/day_num"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="10dp"
            android:background="#000000"
            android:height="35dp"
            />
    
    
    </LinearLayout>
    

    【讨论】:

    • 如果您不介意我的提问,那究竟为什么需要这样做?哦,它没有用。根本不动。
    【解决方案4】:

    我遇到了同样的问题,使用 centerInParent 的 RelativeLayout 也没有帮助。

    基本上,除法可能包含将根据屏幕宽度截断的余数。

    因此,使 GridView 居中的唯一方法是计算一个不带余数的宽度,并使用余数作为填充。

    WindowManager manager = window.getWindowManager();
    int  screenWidth      = manager.getDefaultDisplay().getWidth();
    int contentWidth      = screenWidth;
    
    while( 0 != contentWidth%COLUMN_NUMBER ) contentWidth--;
    
    columnWidth = contentWidth / COLUMN_NUMBER;
    int padding = screenWidth - contentWidth;
    

    然后,当您创建 GridView 时:

    gridView.setColumnWidth(columnWidth);
    gridView.setPadding(padding / 2, 0, padding / 2, 0);
    

    【讨论】:

      【解决方案5】:
      There is more specific method to do this. You can define custom layout. And add TextView as the element. Then apply the properties what you want. To center make gravity = center like wise. Then apply that layout as the layout for your grid view.
      
      Ex :
      Sample layout name : myCellitemlayout.xml
      <TextView>
            android:id="@+id/text_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
      </TextView>
      
      Now add this to your gridView adapter.
      Ex: GridView
      
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                      R.id.myCellitemlayout, numbers);
      
      gridView.setAdapter(adapter);
      

      【讨论】:

        【解决方案6】:

        centerInParent 也不适合我。

        将 GridView 的 layout_height 属性更改为 wrap_content,如下所示:

        <GridView
            android:id="@+id/calendar_grid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="7"
            android:columnWidth="40dp"
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"
            android:paddingTop="2dp"
            android:stretchMode="columnWidth"
            android:layout_gravity="right"
            android:gravity="right"
            android:background="#696969" ></GridView>
        

        如果您使用的是 RelativeLayout 或 LinearLayout 并不重要,但您必须像这样指定重力属性居中:

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
        

        它对我很有效。

        【讨论】:

          猜你喜欢
          • 2016-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-18
          • 2021-07-12
          • 2015-10-12
          • 1970-01-01
          相关资源
          最近更新 更多