scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)
在一个开发任务中接到一个需求,是用scrollview作为父控件,放在最外层使用,但是,这时在scrollview
里边再添加任何子控件时,都无法加载出来,例如:
<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”
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">