1、MainActivity布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
</LinearLayout>
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="gone">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
</LinearLayout>
</FrameLayout>
</android.support.v4.app.FragmentTabHost>
<LinearLayout
android:id="@+id/main_bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#DADADA"/>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp">
<RadioButton
android:id="@+id/information"
style="@style/main_bottom"
android:layout_width="wrap_content"
android:checked="true"
android:drawableTop="@mipmap/ic_launcher"
android:text="一"
android:textColor="#000000"/>
<RadioButton
android:id="@+id/blog"
style="@style/main_bottom"
android:drawableTop="@mipmap/ic_launcher"
android:text="二"
android:textColor="#000000"/>
<RadioButton
android:id="@+id/zxg"
style="@style/main_bottom"
android:drawableTop="@mipmap/ic_launcher"
android:text="三"
android:textColor="#000000"/>
<RadioButton
android:id="@+id/find"
style="@style/main_bottom"
android:drawableTop="@mipmap/ic_launcher"
android:text="四"
android:textColor="#000000"/>
<RadioButton
android:id="@+id/person"
style="@style/main_bottom"
android:drawableTop="@mipmap/ic_launcher"
android:text="五"
android:textColor="#000000"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
注:经运行,FragmentTabHost中包含的以下代码,可以不写。
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
</LinearLayout>
</FrameLayout>
2、在activity中使用
MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
// 设置显示的标题
TabHost.TabSpec tabSpecA = mTabHost.newTabSpec("0").setIndicator("A");
TabHost.TabSpec tabSpecB = mTabHost.newTabSpec("1").setIndicator("B");
TabHost.TabSpec tabSpecC = mTabHost.newTabSpec("2").setIndicator("C");
TabHost.TabSpec tabSpecD = mTabHost.newTabSpec("3").setIndicator("D");
TabHost.TabSpec tabSpecE = mTabHost.newTabSpec("4").setIndicator("E");
// 添加对象
mTabHost.addTab(tabSpecA, OneFragment.class, null);
mTabHost.addTab(tabSpecB, TwoFragment.class, null);
mTabHost.addTab(tabSpecC, ThreeFragment.class, null);
mTabHost.addTab(tabSpecD, FourFragment.class, null);
mTabHost.addTab(tabSpecE, FiveFragment.class, null);
mMainGroup = (RadioGroup) findViewById(R.id.radioGroup1);
mMainGroup.setOnCheckedChangeListener(this);
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.information:
mTabHost.setCurrentTabByTag("0");
break;
case R.id.blog:
mTabHost.setCurrentTabByTag("1");
break;
case R.id.zxg:
mTabHost.setCurrentTabByTag("2");
break;
case R.id.find:
mTabHost.setCurrentTabByTag("3");
break;
case R.id.person:
mTabHost.setCurrentTabByTag("4");
break;
default:
break;
}
}
**接下来是fragment的生命周期,因为fragment是依赖于activity的,所以一起打印。
这里测试了4种常见情况:**
1、启动-home-清缓存
启动
---activity---onCreate---
---activity---onStart---
---activity---onResume---
---Fragment---onAttach---
---Fragment---onCreate---
---Fragment---onCreateView---
---Fragment---onActivityCreated---
---Fragment---onStart---
---Fragment---onResume---
按下home
---Fragment---onPause---
---activity---onPause---
---Fragment---onStop---
---activity---onStop---
再次回到界面
---Fragment---onStart---
---activity---onStart---
---activity---onResume---
---Fragment---onResume---
按下home
---Fragment---onPause---
---activity---onPause---
---Fragment---onStop---
---activity---onStop---
清缓存
---Fragment---onDestroyView---
---Fragment---onDestroy---
---Fragment---onDetach---
---activity---onDestroy---
2、启动-第一个fragment-另一个
启动-默认显示第一个fragment
---activity---onCreate---
---activity---onStart---
---activity---onResume---
---Fragment---onAttach---
---Fragment---onCreate---
---Fragment---onCreateView---
---Fragment---onActivityCreated---
---Fragment---onStart---
---Fragment---onResume---
另一个
---Fragment---onPause---
---Fragment---onStop---
---Fragment---onDestroyView---
---另一个Fragment---onAttach---
---另一个Fragment---onCreate---
---另一个Fragment---onCreateView---
---另一个Fragment---onActivityCreated---
---另一个Fragment---onStart---
---另一个Fragment---onResume---
回到第一个
---另一个Fragment---onPause---
---另一个Fragment---onStop---
---另一个Fragment---onDestroyView---
---Fragment---onCreateView---
---Fragment---onActivityCreated---
---Fragment---onStart---
---Fragment---onResume---
home
---Fragment---onPause---
---activity---onPause---
---Fragment---onStop---
---activity---onStop---
清缓存
---Fragment---onDestroyView---
---Fragment---onDestroy---
---Fragment---onDetach---
---另一个Fragment---onDestroy---
---另一个Fragment---onDetach---
---activity---onDestroy---
3、启动-锁屏-屏幕亮-进入界面-home-清缓存
启动
---activity---onCreate---
---activity---onStart---
---activity---onResume---
---Fragment---onAttach---
---Fragment---onCreate---
---Fragment---onCreateView---
---Fragment---onActivityCreated---
---Fragment---onStart---
---Fragment---onResume---
锁屏
---Fragment---onPause---
---activity---onPause---
---Fragment---onStop---
---activity---onStop---
屏幕亮
没变化
解锁进入界面
---Fragment---onStart---
---activity---onStart---
---activity---onResume---
---Fragment---onResume---
home
---Fragment---onPause---
---activity---onPause---
---Fragment---onStop---
---activity---onStop---
清缓存
---Fragment---onDestroyView---
---Fragment---onDestroy---
---Fragment---onDetach---
---activity---onDestroy---
4、跳转到另一个activity,然后另一个activity被finish
跳转
---Fragment---onPause---
---activity---onPause---
---Fragment---onStop---
---activity---onStop---
回来
---Fragment---onStart---
---activity---onStart---
---activity---onResume---
---Fragment---onResume---
home
---Fragment---onPause---
---activity---onPause---
---Fragment---onStop---
---activity---onStop---
清缓存
---Fragment---onDestroyView---
---Fragment---onDestroy---
---Fragment---onDetach---
---activity---onDestroy---