android scrollView 内部子布局MATCH_PARENT却无法填充满屏幕问题

本文探讨了ScrollView内部元素未填充全屏的问题,解释了原因并提供了解决方案,即在ScrollView中设置android:fillViewport=true。通过实例演示了如何在ScrollView中正确配置布局,以确保内部元素能充分利用可用空间。

ScrollView滚动视图是指当拥有很多内容、屏幕显示不完时、需要通过滚动跳来显示的视图、Scrollview的一般用法如下、以下代码在Scrollview里面放了一个RelativeLayout、并且是设置为android:layout_height="match_parent"填充全屏的,但是测试以后不起作用。布局如下:

<ScrollView
	android:layout_width="match_parent"
	android:layout_height="match_parent" >

	<RelativeLayout
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:background="@color/common_background" >

		<TextView
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_centerHorizontal="true"
			android:layout_alignParentBottom="true"
			android:background="@drawable/bottom_bg" />

	</RelativeLayout>
</ScrollView>
查看android API 获知,

其中白色部分是scrollView,灰色部分是TextView,很明显,scrollview(白色)已经扩展到最大高度了,但是其内部的TextView(灰色)却没有扩展.可明明TextView的layout_height="fill_parent",为什么没占满呢?是因为TextView的上层LinearLayout为wrap_content的原因吗?

但是换成fill_parent还是一样的(实际上Scrollview的第一层View的layout_weight在sdk中是建议为wrap_content的)。



后来在stackoverflow上找到了原因: http://stackoverflow.com/questions/2599837/linearlayout-not-expanding-inside-a-scrollview

要让ScrollView内部元素的 fill_parent 起作用必须设置android:fillViewport="true"

1
2
3
4
5
<ScrollView
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:fillViewport= "true"
>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值