【问题标题】:Center elements in GridViewGridView 中的中心元素
【发布时间】:2011-06-16 12:07:08
【问题描述】:

我有 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"
/>

【问题讨论】:

    标签: android gridview


    【解决方案1】:

    这是我为使 gridview 中的项目居中所做的工作(注意拉伸模式、列宽、重力以及水平和垂直间距):

    <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>
    

    我花了一些时间才弄明白,但在弄乱这些不同的值时,我能够让它们居中。

    【讨论】:

    • 对我来说 HorizontalSpacing 导致了 spacingWidthUnfirom 的一些问题,但在我删除它之后就很好了
    • 我发现 android:layout_centerHorizo​​ntal="true" 为我做到了
    • @T0m 你能告诉我们你把它放在哪个标签上吗?
    • 这是 GridView 标签
    • 是的,我认为 android:horizontalSpacing="10dp" 为我完成了这项工作!
    【解决方案2】:

    尝试将您的 GridView 包装在使其子元素居中的 LinearLayout 中,并将您的 gridview 的 layout_height 更改为“wrap_content”。例如:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_height="fill_parent"
      android:layout_width="fill_parent"
      android:gravity="center"
      android:layout_gravity="center">
        <GridView 
          android:id="@+id/homeGridView"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:numColumns="3"
          android:verticalSpacing="10dp"
          android:horizontalSpacing="10dp"
          android:stretchMode="columnWidth"
          android:gravity="center" />
    </LinearLayout>
    

    【讨论】:

    • 在我能找到的所有 SO 中,这是唯一对我有用的答案
    【解决方案3】:

    需要设置以下属性:

    android:stretchMode="columnWidth"
    

    ...和...

    android:gravity="center"
    

    【讨论】:

      【解决方案4】:

      好吧,如果您使用自定义适配器来膨胀 gridview 中的单个 rowview,然后尝试将 android:gravity="center" 添加到您的 single_row 的布局中......这样就可以了希望的诀窍:) 即,

      <?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:gravity="center"
          android:orientation="vertical" >
      
          <ImageView
              android:id="@+id/ivClrBlock"
              android:layout_width="88dp"
              android:layout_height="88dp"
              android:layout_marginBottom="5dp"
              android:layout_marginTop="5dp"
              />
      
          <TextView
              android:id="@+id/tvClrName"
              android:layout_width="88dp"
              android:layout_height="wrap_content"
              android:layout_marginBottom="5dp"
              android:layout_marginTop="5dp"
              android:gravity="center"
              android:text="@string/color_name"
              android:textSize="15sp" />
      
      </LinearLayout>
      

      【讨论】:

        【解决方案5】:

        当我从以下位置更新 gridview 适配器时出现问题:

                gridView = inflater.inflate(R.layout.mobile, null);
        

                gridView = inflater.inflate(R.layout.mobile, parent, false);
        

        所以如果旧 (null) 版本失败,我只是使用 try catch 来反转。

        【讨论】:

          【解决方案6】:

          尝试改变这个:
          android:layout_height="fill_parent"
          转入
          android:layout_height="wrap_content"

          您现在告诉您的网格视图完全填充它的父级高度和宽度。

          【讨论】:

          • 谢谢。我尝试过这种方式,但这并不能解决问题,元素不会在 GridView 内垂直居中。
          • 我有点困惑,但我会尽力提供帮助。您是否尝试过将 GridView 父级设置为使其所有元素居中?您也可以尝试将重力属性更改为其他属性,看看是否有帮助。这是可能属性的链接developer.android.com/reference/android/widget/…
          【解决方案7】:

          这是我的答案:

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@id/linearLayout"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal">
              <GridView
                  android:id="@+id/gridView"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:columnWidth="100dip"
                  android:gravity="center"
                  android:numColumns="3"
                  android:stretchMode="spacingWidthUniform"
                  android:verticalSpacing="10dip" />
          </LinearLayout>
          

          如果您不需要,请勿使用paddinghorizontalSpacing,因为这可能会导致其他视图出现问题。

          另请注意,fill_parent 已被弃用。

          【讨论】:

            【解决方案8】:

            希望这能解决你的问题:

              <LinearLayout
                    android:layout_gravity="center_horizontal"
                    android:gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >
                <GridLayout
                    android:id="@+id/gridlayou4x3"
                    android:rowCount="4"
                    android:columnCount="3"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="80"
                    android:minWidth="25px"
                    android:minHeight="25px" />
              </LinearLayout>
            

            【讨论】:

              【解决方案9】:

              我发现您在 heightwidth 上只有 gridlayout 包装内容。

              android:layout_centerHorizontal="true"
              

              它会以它需要的大小居中,但如果你让它匹配父级。您在技术上以大量父级空白空间抵消您的子级为基础。

              【讨论】:

                猜你喜欢
                • 2010-11-15
                • 2011-10-12
                • 2012-10-09
                • 1970-01-01
                • 2014-05-28
                • 1970-01-01
                • 1970-01-01
                • 2022-11-19
                • 2016-01-20
                相关资源
                最近更新 更多