GridView 中的中心元素
我有 GridView,其中 2 列有 6 个图像。我想在屏幕中间显示这 6 张图像。我将重力属性设置为中心,但该中心元素仅水平排列。 GridView 占据整个屏幕。
<GridView
android:id="@+id/gridview"
android:layout_above="@id/ad" android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:background="#ffffff"
android:gravity="center"
/>
I have GridView with 6 images in 2 columns. I want to display this 6 images in the middle of the screen. I set gravity properties to center but this center elements only horizontally. GridView takes whole screen.
<GridView
android:id="@+id/gridview"
android:layout_above="@id/ad" android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:background="#ffffff"
android:gravity="center"
/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
以下是我为使 gridview 中的项目居中所做的操作(注意拉伸模式、列宽、重力以及水平和垂直间距):
我花了一段时间才弄清楚,但弄乱了我能够得到的那些不同值他们到中心。
Here is what I did to get the items in the gridview to center (notice the stretchmode, columnwidth, gravity and horizontal and vertical spacing):
It took me a while to figure it out, but messing with those different values I was able to get them to center.
尝试将 GridView 包装在其子元素居中的 LinearLayout 中,并将 gridview 的layout_height 更改为“wrap_content”。前任:
Try wrapping your GridView in a LinearLayout that has it's child elements centered and change your gridview's layout_height to "wrap_content". ex:
需要设置以下属性:
...和...
Need to set the following attributes:
...and...
好吧,如果您使用自定义适配器来膨胀 gridview 中的单个 rowview,则尝试将 android:gravity="center" 添加到 single_row 的布局中。希望这能解决问题:)
IE,
well, in case you are using a custom adapter thing to inflate single rowview inside your gridview then try to add android:gravity="center" to the layout of your single_row.. this will do the trick hopefully :)
i.e,
当我将 gridview 适配器从: 更新为 时出现问题,
因此
如果旧(空)版本失败,我只是使用 try catch 反转回来。
Issue occurred when I updated the gridview adapter from:
to
so I just reversed back using a try catch if the old (null) version failed.
尝试改变这一点:
android:layout_height="fill_parent"
进入
android:layout_height="wrap_content"
您现在告诉网格视图完全填充其父级高度和宽度。
Try to change this:
android:layout_height="fill_parent"
into
android:layout_height="wrap_content"
You are now telling your grid view to fill it's parent height and width fully.
这是我的答案:
如果不需要,请不要使用
padding
或horizontalSpacing
,因为它可能会导致其他视图出现问题。另请注意,
fill_parent
已被弃用。Here is my answer:
Don't use
padding
orhorizontalSpacing
if you don't need it as it may cause problems with other views.Note also that
fill_parent
is already deprecated.我希望这能解决您的问题:
I hope this will solve your problem:
我发现您只有
gridlayout
在height
和width
上包裹内容。它会根据需要的大小自行居中,但如果它与父级相匹配。从技术上讲,您将其居中,这是基于大量父母的空白空间抵消了您的孩子。
I found that you just have the
gridlayout
wrap content onheight
andwidth
.It will center itself for the size it needs but if you have it matching the parent. You are technically centering it based with a lot of parent empty space offsetting your children.