ScrollView 嵌套 ViewPager,ViewPager内容不显示问题

来源:互联网 发布:程序员兼职的平台 编辑:程序博客网 时间:2024/05/06 09:20

解决方法:

在ViewPager 外面嵌套ScrollView 时导致ViewPager 中内容不显示,解决的办法是在ScrollView 标签下加入 android:fillViewport="true" ,顾名思义,这个属性允许 ScrollView中的组件去充满它。

原因分析:

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来计算.

本质:ScrollView中不能使用如下属性设置空间的高度

android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1">


例如,在ScrollView下加入如下组件,无论如何也不能自动扩展到屏幕高度。

布局文件。

<?xml version="1.0" encoding="utf-8"?> <!-- 背景:蓝色 --> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/scrollView1"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:background="#0000ff" >      <!-- 背景:绿色 -->      <LinearLayout         android:id="@+id/linearLayout1"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:background="#00ff00" >          <TextView             android:id="@+id/textView1"             android:layout_width="wrap_content"             android:layout_height="fill_parent"             android:layout_weight="1"             android:text="Hello Android." >         </TextView>     </LinearLayout>  </ScrollView> 
效果图

ScrollView中添加一个android:fillViewport=&quot;true&quot;

尽管已经设置了android:layout_height="fill_parent",但是,整个LinearLayout和TextView还是不能充满整个屏幕。解决办法。在ScrollView中添加一个android:fillViewport="true"属性就可以了。顾名思义,这个属性允许ScrollView中的组件去充满它。

修改后的效果图。

ScrollView中添加一个android:fillViewport=&quot;true&quot;

参考:http://www.2cto.com/kf/201208/148262.html

4 0