Android 如何正确居中 GridView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11554027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:01:04  来源:igfitidea点击:

How to center GridView correctly?

androidandroid-gridview

提问by Andy

I have been trying and trying and no such luck. I have lucked at all other answers on here as well related to centering GridViewand also no luck. This is my GridViewxml:

我一直在尝试和尝试,但没有这样的运气。我在这里的所有其他答案以及与居中相关的所有其他答案都很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>

I have tried every combination I can think of, but not much has worked. At least its not consistent between devices. This is an image of what it always does.

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

enter image description here

在此处输入图片说明

As you can see on the right side of the GridView, there is spacing. I try to constantly change that, and it almost never works. Using android:stretchModedoes change it, but it messes up the spacing I have between the cells, so thats not working. Using Gravity also fails as you can see. I've tried all the other values for it and it still changes nothing. It's stuck to the left. I also find it weird that trying to change the size of android:columnWidthfrom anything under 40 it does nothing either. And when I increase 40 to 45, it also changes nothing. But changing it to 50 makes the space on the right go away. But when putting it on my phone, the spacing is there! I skipped this like 2 weeks ago to continue working on other stuff but it looks like I have yet to solve this issue or even understand why nothing I try works.

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

If it helps anyone help me out here I can also provide the xml for what makes the cells. But I am not sure if it matters. But here it is:

如果它可以帮助任何人在这里帮助我,我还可以提供用于制作单元格的 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>

If it helps, the GridView's parent is a LinearLayoutwith this:

如果有帮助,则GridView其父级是LinearLayout这样的:

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

There is also a sibling with a weight of 1, but I don't think that should be effecting is as the weight of 0 makes that part set its space first.

还有一个权重为 1 的兄弟姐妹,但我认为这不会产生影响,因为 0 的权重使该部分首先设置其空间。

EDIT:

编辑

So I have kept playing with it, very annoyed, especially since not that many people seem to have this issue, but it looks like no matter how small I make the columnWidth, the GridView barely gets any smaller. In fact, that same gap you see is what it stays at until I go to 50dp. But when I go in portrait mode the gap reappears, so I raise it up to 90dp (I tried under that, didn't change anything), and it goes away. Weird thing is the GridViewdoesn't change besides the filling of the gap. So I could make it 200dp, and it would look perfect on my screen dimen, and any other smaller screen. Once I go to a tablet, I suspect I would need to increase that more, but the point is, this is weird behavior. Has anyone encountered this?

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

采纳答案by Aleks G

You may want to put your GridViewinside a RelativeLayoutand then set GridView's layout_centerInParentproperty to true. something like this:

你可能想把你的GridView里面 aRelativeLayout然后设置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>

RelativeLayoutgives you much better control over positions of children.

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

回答by Roger Alien

Here is my working configuration:

这是我的工作配置:

        <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"
        />

回答by jeet

you need to set Gravity of Parent Layout of TextView or LayoutGravity of TextView, Plz change Cell Layout as follows:

你需要设置TextView的Parent Layout的Gravity或TextView的LayoutGravity,请按如下方式更改单元格布局:

<?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>

回答by shi

I've encountered the same problem and RelativeLayout with centerInParent did not help either.

我遇到了同样的问题,并且带有 centerInParent 的 RelativeLayout 也没有帮助。

Basically, the division may contain a remainder that will be cut off according to the screen width.

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

so, the only way to center the GridView was to calculate a width that would fit with no remainder and use the remainder value as the padding.

因此,将 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;

then, when you create your GridView:

然后,当您创建 GridView 时:

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

回答by Neyomal

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);

回答by Big Disgrace

centerInParent didn't work for me either.

centerInParent 也不适合我。

Change GridView's layout_height property to wrap_content like this:

像这样将 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>

It doesn't much matter if you are using RelativeLayout or LinearLayout but you have to specify that gravity property to center like this:

如果您使用 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">

It is working for me very well.

它对我很有效。