Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 垂直回收器视图中的列未居中_Android_Android Layout_Android Recyclerview_Gridlayoutmanager - Fatal编程技术网

Android 垂直回收器视图中的列未居中

Android 垂直回收器视图中的列未居中,android,android-layout,android-recyclerview,gridlayoutmanager,Android,Android Layout,Android Recyclerview,Gridlayoutmanager,左侧和右侧,我在列和屏幕边缘之间有不同的距离。但我希望看到他们也一样 当前状态 Item.xml <com.google.android.material.card.MaterialCardView android:layout_width="wrap_content" android:layout_height="wrap_content"> ... </com.google.android.material.card.MaterialCardVie

左侧和右侧,我在列和屏幕边缘之间有不同的距离。但我希望看到他们也一样

当前状态

Item.xml

<com.google.android.material.card.MaterialCardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    ...
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
     android:layout_width="match_parent"
     android:layout_height="match_parent">
     ...
      <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            app:spanCount="4"
            .../>
</LinearLayout>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
        ...
    </com.google.android.material.card.MaterialCardView>
</FrameLayout>

...
Layout.xml

<com.google.android.material.card.MaterialCardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    ...
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
     android:layout_width="match_parent"
     android:layout_height="match_parent">
     ...
      <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            app:spanCount="4"
            .../>
</LinearLayout>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
        ...
    </com.google.android.material.card.MaterialCardView>
</FrameLayout>

android:layout\u width=“匹配父项”
android:layout\u height=“match\u parent”>
...

我通过添加一个具有匹配父宽度的根元素并设置嵌入视图的水平中心来解决这个问题

Item.xml

<com.google.android.material.card.MaterialCardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    ...
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
     android:layout_width="match_parent"
     android:layout_height="match_parent">
     ...
      <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            app:spanCount="4"
            .../>
</LinearLayout>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
        ...
    </com.google.android.material.card.MaterialCardView>
</FrameLayout>

...
因此: