RecyclerView 高度 wrap_content 失效的问题

本文详细介绍了在遇到RecyclerView设置高度为wrap_content时失效的问题,特别是当使用GridLayoutManager且只显示一行数据的情况。作者指出这可能是谷歌的一个BUG,并提供了一个有效的修复方案,通过在RecyclerView外层包裹一个布局,将RecyclerView的高度设置为外层布局的高度来解决问题。

转载请注明链接:https://blog.youkuaiyun.com/feather_wch/article/details/88293585

试过很多办法,但是都是标题党。本问题绝对可以解决。

RecyclerView 高度 wrap_content 失效的问题

版本:2019-03-07(12:30)

RecyclerView的属性android:layout_height="wrap_content"出现了失效的问题,我是用的GridLayoutManager,结果只有一行的数据。

该问题是谷歌的BUG

修复方法

1、修复方法

  1. RelativeLayout包裹RecyclerView,RelativeLayout的高度采用wrap_content
  2. RecyclerView的高度,采用match_parent

如下:

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/home_videos_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="ExtraText"> <!--图片按钮--> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_server"/> <!--开关按钮--> <ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content"/> <!--选择按钮--> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content"/> <!--下拉列表--> <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgroundTint="@color/black"/> <!--引用自定义形状--> <!-- <ImageView android:src="@drawable/sb" 这个属性的作用是让视图能够响应点击事件。 android:clickable="true" --> 这个属性的功能是将视图的点击事件和 Activity 里的方法关联起来。 android:onClick="loginWithQQ" --> 使用 CardView 实现圆角和阴影效果 每个按钮使用CardView作为容器 通过app:cardCornerRadius="12dp"设置圆角 通过app:cardElevation="4dp"添加阴影 设置app:cardBackgroundColor替代原有的backgroundTint <!--<权重分配> android:layout_weight="1" android:layout_weight="2" android:layout_weight="3" </权重分配>--> <!--垂直下滑格式--> <!--垂直下滑容器--> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0000000000" android:textSize="20sp" android:textColor="@color/black"/> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:src="@drawable/hutao" android:scaleType="centerCrop"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0000000000" android:textSize="20sp" android:textColor="@color/black"/> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:src="@drawable/hutao" android:scaleType="centerCrop"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0000000000" android:textSize="20sp" android:textColor="@color/black" /> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:src="@drawable/hutao" android:scaleType="centerCrop"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0000000000" android:textSize="20sp" android:textColor="@color/black"/> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:src="@drawable/hutao" android:scaleType="centerCrop"/> </LinearLayout> </ScrollView> <!--垂直下滑容器--> </LinearLayout>错误分析
08-02
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <com.example.styledome.FullScreenVideoView android:id="@+id/video" android:layout_width="@dimen/dp_640" android:layout_height="@dimen/dp_370" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="@dimen/dp_370" android:layout_toRightOf="@id/video" android:orientation="vertical" android:background="@mipmap/bg_weather" > <TextView android:id="@+id/tv_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:textSize="@dimen/sp_23" android:text="2025-06-09" android:layout_alignParentRight="true" android:layout_gravity="center" android:layout_marginTop="@dimen/dp_23" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:text="7:00" android:textSize="@dimen/sp_40" android:layout_gravity="center" android:layout_marginTop="@dimen/dp_17" /> <ImageView android:id="@+id/iv_image" android:layout_width="@dimen/dp_130" android:layout_height="@dimen/dp_91" android:layout_gravity="center" android:layout_marginTop="@dimen/dp_9" android:src="@mipmap/duoyun" /> <TextView android:id="@+id/tv_weather" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:textSize="@dimen/sp_30" android:layout_gravity="center" android:text="晴转多云" android:layout_marginTop="@dimen/dp_19" /> <TextView android:id="@+id/tv_temperature" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:textSize="@dimen/sp_60" android:layout_gravity="center" android:text="24" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" > <TextView android:id="@+id/tv_week" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:textColor="@color/white" android:visibility="gone" android:textSize="11sp" /> </LinearLayout> </LinearLayout> </RelativeLayout> <!-- <RelativeLayout android:layout_width="wrap_content" android:layout_height="@dimen/dp_67"> <ImageView android:layout_width="@dimen/dp_34" android:layout_height="@dimen/dp_31" android:layout_marginTop="@dimen/dp_16" android:layout_marginLeft="@dimen/dp_27" android:src="@mipmap/icon_bulletin" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/bg_bulletin" android:layout_marginLeft="@dimen/dp_39" android:text="公交出行,节能减排,保护环境人人有责!" android:textColor="@color/white" android:textSize="@dimen/sp_36" android:gravity="center" /> </RelativeLayout>--> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rec" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/dp_10" android:layout_marginLeft="@dimen/dp_16" android:layout_marginRight="@dimen/dp_16" android:layout_marginBottom="@dimen/dp_11" /> </LinearLayout> <com.example.styledome.VerticalMarqueeTextView android:id="@+id/tv_marquee" android:layout_width="40dp" android:layout_height="@dimen/dp_370" android:textColor="#000" android:textSize="16sp" android:padding="4dp" /> </FrameLayout>设置高度不生效 <com.example.styledome.VerticalMarqueeTextView android:id="@+id/tv_marquee" android:layout_width="40dp" android:layout_height="@dimen/dp_370" android:textColor="#000" android:textSize="16sp" android:padding="4dp" />
07-16
你这样改没用,他还是那样跟原来没变化。原来是这样<?xml version="1.0" encoding="utf-8"?> <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:fitsSystemWindows="true" android:layout_marginTop="?attr/actionBarSize"> <!-- 根布局:ConstraintLayout 必须 match_parent 高度才能被 NestedScrollView 正确测量 --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 🔵 垂直指南线:5%、95% --> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.05" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_15" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.15" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_95" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.95" /> <!-- 🔍 输入框 --> <EditText android:id="@+id/search_input" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="请输入需要查询的公交线路或站点" android:textColorHint="#777777" android:textColor="@color/black" android:background="@drawable/rounded_edittext" android:minHeight="48dp" android:textSize="16sp" android:padding="12dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@id/search_btn" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintTop_toTopOf="@id/guideline_05" app:layout_constraintBottom_toTopOf="@id/space_after_search" android:layout_marginStart="16dp" android:layout_marginEnd="8dp" /> <!-- 🔎 搜索按钮 --> <Button android:id="@+id/search_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索" app:layout_constraintTop_toTopOf="@id/guideline_05" app:layout_constraintBottom_toTopOf="@id/space_after_search" app:layout_constraintStart_toEndOf="@id/search_input" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp" /> <!-- ⬛ Space: 搜索栏下方留出 5% 屏幕高度作为间隔 --> <Space android:id="@+id/space_after_search" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/guideline_15" app:layout_constraintBottom_toTopOf="@id/map_view" app:layout_constraintHeight_percent="0.05" /> <!-- 🗺️ 地图视图:占页面 40% 高度 --> <com.amap.api.maps.MapView android:id="@+id/map_view" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/space_after_search" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHeight_percent="0.4" /> <!-- ⬛ Space: 地图到列表之间的空白 --> <Space android:id="@+id/space_after_map" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/map_view" app:layout_constraintBottom_toTopOf="@id/result_list" app:layout_constraintHeight_percent="0.03" /> <!-- 🔽 RecyclerView:结果列表,占据剩余空间 --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/result_list" android:textColorHint="#777777" android:textColor="@color/black" android:layout_width="0dp" android:layout_height="wrap_content" android:background="@drawable/rounded_edittext" app:layout_constraintTop_toBottomOf="@id/space_after_map" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toTopOf="@id/btn_go_to" android:layout_marginHorizontal="16dp" /> <!-- “到这去”按钮 --> <Button android:id="@+id/btn_go_to" android:layout_width="0dp" android:layout_height="wrap_content" android:text="到这去" android:layout_margin="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="@id/guideline_95" /> <!-- 🌀 加载进度条:叠加在屏幕中央,不破坏原布局 --> <ProgressBar android:id="@+id/progress_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:elevation="10dp" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.core.widget.NestedScrollView>
最新发布
11-08
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值