GridView 中的中心元素 [英] Center elements in GridView
本文介绍了GridView 中的中心元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 GridView 有 2 列 6 个图像.我想在屏幕中间显示这 6 个图像.我将重力属性设置为中心,但此中心元素仅水平放置.GridView 占据整个屏幕.
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"
/>
推荐答案
这是我为了让 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):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dp" />
</LinearLayout>
我花了一些时间才弄明白,但是通过处理这些不同的值,我能够让它们居中.
It took me a while to figure it out, but messing with those different values I was able to get them to center.
这篇关于GridView 中的中心元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文