我一直在努力,没有运气。我很幸运能在这里找到所有其他答案,而且与以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>
我已经尝试了所有我能想到的组合,但效果并不显着。至少设备之间的不一致。这是它总是做的图像。
如您所见,在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
内,然后将GridView
的layout_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
使您可以更好地控制 child 的位置。