Android 垂直回收器视图中的列未居中
左侧和右侧,我在列和屏幕边缘之间有不同的距离。但我希望看到他们也一样 当前状态 Item.xmlAndroid 垂直回收器视图中的列未居中,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
<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>
...
因此: