如何使用 XML 和 Layout 对齐 gridView?

How to align gridView using XML and Layout?

如何使用 XML 和 Layout 对齐 gridView?

我尝试了很多属性和方法,对齐只发生在水平方向,我需要和垂直

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="1" 
        android:gravity="center"
        android:layout_gravity="center">
    <GridView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/gridView"
            android:numColumns="3"
            android:scrollbars="none"
            android:overScrollMode="never"
            android:gravity="center"/>

您可以尝试关注

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gridView"
        android:numColumns="3"
        android:scrollbars="none"
        android:overScrollMode="never"
        android:layout_centerInParent="true"/>
</RelativeLayout>

对于线性,您可以使用

<LinearLayout
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

<GridView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gridView"
    android:numColumns="3"
    android:scrollbars="none"
    android:overScrollMode="never"/>

</LinearLayout>
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:gravity="center">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gridView"
        android:numColumns="3"
        android:scrollbars="none"
        android:overScrollMode="never"
        android:layout_centerInParent="true"/>
</RelativeLayout>