您的位置:首页 > 其它

ViewPager中嵌套Fragment不显示问题

2016-06-13 11:36 411 查看
今天遇到一个奇葩问题,原因也很令人崩溃

原来的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mymusic.MainActivity" >

<include layout="@layout/main_top" />

<android.support.v4.view.ViewPager
android:id="@+id/main_content_viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>

</LinearLayout>

这是主布局文件,看着是不是没什么问题,界面也能显示出来

程序运行时发现中间嵌套的Fragment界面没显示出来,是空白的,本来一直认为是控制代码哪里错了,找了半天也没找出来哪里错了。

最后才发现是上面的布局文件的问题。正确的是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.mymusic.MainActivity" >

<include layout="@layout/main_top" />

<android.support.v4.view.ViewPager
android:id="@+id/main_content_viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>

</LinearLayout>

少写了一句代码:如果不填上下面的这句代码,LinearLayout默认是水平布局的,中间区域的ViewPager当然显示不出来了
android:orientation="vertical"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: