ScrollView嵌套LinearLayout布局不能撑满全屏的解决方式

版权声明:转载请注明出处 https://blog.csdn.net/l707941510/article/details/89921486

当ScrollView里的元素想填满ScrollView时,使用"fill_parent"或者"match_parent"是不管用的,必需为ScrollView设置:android:fillViewport="true"。  

如果ScrollView中的元素高度超过当前屏幕的高度时,这个设置将毫无意义。但是我们不能保证每个使用ScrollView的布局都能撑满整个屏幕,所以此时设置android:fillViewport="true"就是必然的了

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
</ScrollView

 当ScrollView没有fillVeewport=“true”时, 里面的元素(比如LinearLayout)会按照wrap_content来计算(不论它是否设了"fill_parent"或者"match_parent"都是无效的),而如果LinearLayout的元素设置了match_parent,那么也是不管用的,因为LinearLayout依赖里面的元素,而里面的元素又依赖LinearLayout,这样自相矛盾.所以里面元素设置了match_parent,也会当做wrap_content来计算.

猜你喜欢

转载自blog.csdn.net/l707941510/article/details/89921486