android - 如何正确居中 GridView?

标签 android android-gridview

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

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

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

enter image description here

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

如果它能帮助任何人帮助我,我也可以提供构成细胞的 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,它在我的屏幕尺寸和任何其他较小的屏幕上看起来都很完美。一旦我使用平板电脑,我怀疑我需要增加更多,但关键是,这是一种奇怪的行为。有人遇到过吗?

最佳答案

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

<?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 - 如何正确居中 GridView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11554027/

相关文章:

android - ExpandableListView 中的方向更改折叠子项

android - 当我在 Gridview 中的 View 项目上放置图像时如何修复 gridview 项目大小(宽度和高度)

android - 7 英寸平板电脑和手机的实际图像尺寸

android - GridView 内的项目在上下滚动时再次加载

java android - 在不同屏幕尺寸的图像背景上调整项目

android - react native 启动画面 : Unfortunately <APP> has stopped after SplashScreen. show()

android - 如何将文件导入android模拟器上的sdcard?

android - ClassCastException : android. 图形.Bitmap & ArrayList<>

android - 将 gridview 项目可检查状态传播给 child

android - Google Maps Android,检测位置感知 map 中的用户交互