GridView 中的中心元素

发布于 2024-10-14 06:27:27 字数 529 浏览 15 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

合约呢 2024-10-21 06:27:27

以下是我为使 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>

我花了一段时间才弄清楚,但弄乱了我能够得到的那些不同值他们到中心。

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.

蓝梦月影 2024-10-21 06:27:27

尝试将 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>

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:

<?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>
池予 2024-10-21 06:27:27

需要设置以下属性:

android:stretchMode="columnWidth"

...和...

android:gravity="center"

Need to set the following attributes:

android:stretchMode="columnWidth"

...and...

android:gravity="center"
贵在坚持 2024-10-21 06:27:27

好吧,如果您使用自定义适配器来膨胀 gridview 中的单个 rowview,则尝试将 android:gravity="center" 添加到 single_row 的布局中。希望这能解决问题:)
IE,

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

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,

<?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>
风流物 2024-10-21 06:27:27

当我将 gridview 适配器从: 更新为 时出现问题,

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

因此

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

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

Issue occurred when I updated the gridview adapter from:

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

to

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

so I just reversed back using a try catch if the old (null) version failed.

戏剧牡丹亭 2024-10-21 06:27:27

尝试改变这一点:
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.

这个俗人 2024-10-21 06:27:27

这是我的答案:

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

如果不需要,请不要使用 paddinghorizo​​ntalSpacing,因为它可能会导致其他视图出现问题。

另请注意,fill_parent 已被弃用。

Here is my answer:

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

Don't use padding or horizontalSpacing if you don't need it as it may cause problems with other views.

Note also that fill_parent is already deprecated.

葬﹪忆之殇 2024-10-21 06:27:27

我希望这能解决您的问题:

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

I hope this will solve your problem:

  <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>
小草泠泠 2024-10-21 06:27:27

我发现您只有 gridlayoutheightwidth 上包裹内容。

android:layout_centerHorizontal="true"

它会根据需要的大小自行居中,但如果它与父级相匹配。从技术上讲,您将其居中,这是基于大量父母的空白空间抵消了您的孩子。

I found that you just have the gridlayout wrap content on height and width.

android:layout_centerHorizontal="true"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文