在app界面设计中,常常遇到 一个控件不能全部显示 想要显示的内容,就不需要用到滚动条ScrollView这个控件,而想要使得界面设计符合你的设计界面,却常常又遇到一个问题,当在ScrollView中内嵌 WebView、ListView等 自带 滚动效果的控件时,就会出现冲突。比如最为常见的 ScrollView内嵌ListView问题,百度下,可以看到很多相关内容,如下:
在这里 我恰好遇到的问题是:
使用开源控件:PullToRefreshScrollView来内嵌ListView,界面布局如下:
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
ptr:ptrAnimationStyle="flip" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginBottom="3dip"
android:background="#dcdcdc"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:layout_marginTop="5dip"
android:text="@string/list_title" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:dividerHeight="1dp" />
</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
本来要实现的是想要实现的效果图:
TextView 置顶,ListView根据手机屏幕填充剩余的空间,ListView如果列表太长,就会自动带有滚动功能。
结果效果是ListView只显示单行,带有滚动功能,和想要的效果天差地别
问题分析:
ScrollView里只放一个元素.当ScrollView里的元素想填满ScrollView时,使用”fill_parent”是不管用的,必需为ScrollView设置:android:fillViewport=”true。 当ScrollView没有fillVeewport=“true”时, 里面的元素(比如LinearLayout)会按照wrap_content来计算(不论它是否设了”fill_parent”),而如果LinearLayout的元素设置了fill_parent,那么也是不管用的,因为LinearLayout依赖里面的元素,而里面的元素又依赖LinearLayout,这样自相矛盾.所以里面元素设置了fill_parent,也会当做wrap_content来计算.
参考连接:
http://zhidao.baidu.com/link?url=e9no8XShio5ep1Xfnb5v7Xuurn8wBbkLFiuErg6iBc5Mv1eAxYpVC6Z_kggAaZJM2Q-_vodYP405pOfoPW6uyqandroid:fillViewport=”true”
当一个高度值不足scrollview的子控件fillparent的时候,单独的定义android:layout_height=”fill_parent”是不起作用的,必须加上fillviewport属性,当子控件的高度值大于scrollview的高度时,这个标签就没有任何意义了。
参考连接:
http://zhidao.baidu.com/link?url=NZyGc2LLqz3Ai6IT_DOrFXdenvYtYfasQ4qv8NvV85YwtE5N07WXTpvRmBBjrja93x82dwlvGdfrhRfwaxpZRLbzyvKFthvtSF5rTm2mWUO
解决方法:
解决方法一:设置ListView为固定高度,不过要提前计算好他的高度,比较麻烦
解决方法二:在PullToRefreshScrollView属性中添加android:fillViewport=”true”
总结:
ScrollView中的组件设置android:layout_height=”fill_parent”不起作用的解决办法
在ScrollView中添加一个android:fillViewport=”true”属性就可以了。顾名思义,这个属性允许 ScrollView中的组件去充满它。
转载请注明出处:http://blog.csdn.net/ljb568838953/article/details/52563452