正文
使用FragmentTabHost,就是先用TabHost“装着”Fragment,然后放进MainActivity里面
2. ViewPager
-
定义
ViewPager是android扩展包v4包中的类
android.support.v4.view.ViewPager
-
作用
左右切换当前的view,实现滑动切换的效果。
注:
1.ViewPager类直接继承了ViewGroup类,和LinearLayout等布局一样,都是一个容器,需要在里面添加我们想要显示的内容。
2.ViewPager类需要PagerAdapter适配器类提供数据,与ListView类似 3.Google官方建议ViewPager配合Fragment使用
3. Fragment
实现步骤
-
在主xml布局里面定义一个FragmentTabHost控件
-
定义底部菜单栏布局
-
定义每个Fragment布局
-
定义每个Fragment的Java类
-
定义适配器以关联页卡和ViewPage
-
定义MainActivity(具体实现请看注释)
工程文件目录
工程文件目录
具体实现实例
步骤1:在主xml布局里面定义一个FragmentTabHost控件
主xml布局:Main_tab_layout.xml
RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
include layout="@layout/main_top" />
android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
FrameLayout
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black" >
FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
android.support.v4.app.FragmentTabHost>RelativeLayout