android - GridView 中的中心元素

标签 android 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"
/>

最佳答案

这是我为使 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/

相关文章:

android - 导航到新 fragment 时删除底部导航

android - android中的 bundle ID

java - 当没有 Internet 连接时在 GridView 中返回 NullPointerException 时出现问题

android - 未找到改造注释。 (参数#1)

android - 在 RetrofitError 中捕获错误并将其传递给 RoboSpice Error

android - Gradle项目同步失败,如何解决?

android - 网格中的 CardView 不在所有侧面显示阴影

c# - 无法从 GridView 中的 TemplateField 获取文本

html - Gridview 宽度和对齐方式

asp.net - 使用 javascript 调整大小的 Gridview 列