Android 在RecyclerView中将每个子视图居中
我正在尝试创建一个由卡片组成的水平Android 在RecyclerView中将每个子视图居中,android,layout-manager,android-recyclerview,Android,Layout Manager,Android Recyclerview,我正在尝试创建一个由卡片组成的水平RecyclerView。我希望每张卡片都显示在屏幕中央,然后我可以水平滚动。 我将LinearLayoutManager设置为水平,如下所示: linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false); recyclerView.setLayoutManager(linearLayoutMana
RecyclerView
。我希望每张卡片都显示在屏幕中央,然后我可以水平滚动。
我将LinearLayoutManager
设置为水平,如下所示:
linearLayoutManager = new LinearLayoutManager(getActivity(),
LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
片段的xml如下所示:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context="com.rubberduck.pairup.fragments.FavoritesFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_favorites"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp"
android:layout_gravity="center"
android:scrollbars="vertical" />
</FrameLayout>
您的卡视图的邮政编码too@belowporverty我已经为卡片视图添加了代码。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv_pair"
cardUseCompatPadding="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
card_view:cardCornerRadius="4dp">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_shirt"
android:layout_width="@dimen/image_width"
android:layout_height="@dimen/image_height"
android:scaleType="centerCrop"
android:src="@drawable/tshirt" />
<ImageView
android:id="@+id/iv_trouser"
android:layout_width="@dimen/image_width"
android:layout_height="@dimen/image_height"
android:layout_toRightOf="@id/iv_shirt"
android:scaleType="centerCrop"
android:src="@drawable/jeans" />
</RelativeLayout>
</android.support.v7.widget.CardView>