Android - GridView 项目居中

Android - GridView item in center

我的 android 应用程序布局之一有 GridView。我正在尝试将 Item 设置在 Gridview 的中心,但它始终位于左侧。我在 Whosebug 中搜索了许多类似的问题,但没有解决我的问题。我的 XML 如下所示。让我知道是否有人可以帮助我在中心制作我的 GridView 项目。

  <FrameLayout
    android:id="@+id/framecaregory"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/second"
    android:layout_alignParentTop="true"
    android:layout_marginTop="?actionBarSize">

    <GridView
        android:id="@+id/gridviewcategory"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/lite_color"
        android:numColumns="5"
        android:visibility="gone">

    </GridView>
</FrameLayout>

谢谢

你想要的东西不依赖于gridLayout,而是依赖于它适配的子布局。首先创建另一个 layout grid_item_row.xml 并输入:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/framecaregory"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:gravity="center"
     android:layout_height="wrap_content">

    <TextView
        android:text="Text in the center"
        android:textAlignment="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

之后将此布局调整为网格布局