我有 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"
/>
最佳答案
这是我为使 gridview 中的项目居中所做的工作(注意拉伸(stretch)模式、列宽、重力以及水平和垂直间距):
<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>
我花了一些时间才弄明白,但在弄乱这些不同的值时,我能够让它们居中。
关于android - GridView 中的中心元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4816004/