ScrollView的子View高度match_parent无效 解决

探讨了ScrollView在Android中如何实现子View填满整个视图的问题,解释了match_parent属性的工作机制,并提供了解决方案,即使用android:fillViewport=true。

背景
ScrollView本身的高度设置为match_parent,其子View的高度也设置为match_parent,自然状态下该子View的高度并不会占满ScrollView的高度。 
原因是match_parent针对一般布局而言,是子view的高度和parent的高度一致,但在ScrollView身上,工作机制并非如此,而是ScrollView的高度随着子View的高度变化而变化(子View高度大于ScrollView时)。在子View高度小于ScrollView高度时,必需在xml里为ScrollView加上android:fillViewport="true",这样子View小于ScrollView高度时就会占满父View.

解决方法
xml里为ScrollView加上android:fillViewport="true"
源码地址: https://github.com/HuaDanJson/NestedscrollRecyclerview

<? xml version = "1.0"encoding = " utf-8" ? > android : layout_width = " match_parent" <LinearLayout xmlns: android = " http://schemas.android.com/apk/res/android" android :layout_height = " match_parent" android: orientation = " vertical" ><HorizontalScrollView android: layout_width = " wrap_content" android:layout_height = " 0dp" android:layout_weight ="1" android:scrollbars = " none" >< LinearLayout android:layout_width = " wrap_content" android:layout_height = " match_parent" android :orientation = " horizontal" ><View android: layout_width = "300dp" android :layout_height = " match_parent" android:background = "@color/red"/ ><View android:layout_width = "300dp" android ;layout_height = " match_parent" android:background = " @color/green"/ ><View android :layout_width ="300dp" android :layout_height = " match_parent" android:background = " @color/blue"/></LinearLayout ></HorizontalScrollView ><ScrollView android :layout_width = " match_parent"android :layout_height ="0dp" android :layout_weight ="1" android : scrollbars = " none" ><LinearLayout android :layout_width = " match_parent" android :layout_height = " wrap_content" android :orientation =" vertical" ><View android :layout_width = " match_parent" android :layout_height ="200dp" android:background =" @color/yellow"/ ><View android:layout_width = " match_parent" android :layout_height ="200dp" android;background =" @color/cyan"/ ><View android :layout_width = " match_parent" android:layout_height ="200dp" android :background = " @color/blue"/> </LinearLayout ></ScrollView>/LinearLayout >修正补全
09-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

张海龙(Jason Zhang)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值