【发布时间】:2019-10-16 13:51:15
【问题描述】:
我想使用 GridLayoutManager 在 recyclerview 中显示列表项,我能够显示数据,但有时 recyclerview 会在行中显示额外的间距,如下所示,那么我该如何避免这种额外的间距。 我也想将 recyclerview 扩展到全高
下面是片段的布局代码
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorShopListingBackground"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="@dimen/margin_124"
android:background="@android:color/white" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_8"
android:layout_marginTop="@dimen/margin_8"
android:layout_marginRight="@dimen/margin_8"
android:layout_marginBottom="@dimen/margin_16"
android:background="@drawable/bg_dashboard_categories"
android:elevation="@dimen/margin_16"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_16"
android:layout_marginRight="@dimen/margin_16"
android:layout_marginTop="@dimen/margin_16"
android:fontFamily="@font/montserrat_bold"
android:textSize="@dimen/text_size_18"
android:textColor="@color/colorHandPickedOffer"
android:text="@string/txt_handpicked_offers" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_16"
android:background="@drawable/bg_dashboard_categories"
android:layout_below="@id/tv_title"
android:layoutAnimation="@anim/grid_layout_animation_from_bottom"/>
</RelativeLayout>
</LinearLayout>
下面是列表项
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/colorCategoryDivider"
android:paddingRight="@dimen/margin_1"
android:paddingEnd="@dimen/margin_1"
android:paddingBottom="@dimen/margin_1"
android:paddingTop="@dimen/margin_1"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="@+id/rl_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/margin_16"
android:minWidth="@dimen/margin_136">
<ImageView
android:id="@+id/iv_category_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/margin_16"
android:src="@drawable/fashion" />
<TextView
android:id="@+id/tv_category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/iv_category_image"
android:fontFamily="@font/montserrat_semi_bold"
android:hint="@string/txt_category_name"
android:layout_marginTop="@dimen/margin_16"
android:gravity="center"
android:textSize="@dimen/text_size_12"
android:textColor="@color/colorCategoryText"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
以下是错误图片
【问题讨论】:
-
设置边距和内边距
-
在您的 RelativeLayout 中将
android:layout_height="wrap_content"更改为android:layout_height="match_parent"以获得完整高度 -
@RubenMeiring 我应该为 recyclerview 或列表项设置边距和填充?
-
为列表项设置边距和内边距,同时检查 recyclerview 上的约束、边距和内边距,玩弄这些直到它看起来像你想要的那样,还要检查项目和回收站视图的高度和宽度
-
从 LinearLayout 中移除 minWidth。
标签: android android-recyclerview gridlayoutmanager