继承自FragmentActivity + FragmentTabHost 布局 + Fragment 内容
1:使用findViewById 获得FragmentTabHost 对象
2:使用setup(this, getSupportFragmentManager(), R.id.realtabcontent)初始化
3:使用addTab(newTabSpec().setIndicator(),Fragment.class, null) 添加Tab标签与内容
如 :特有应用需要的Tab标签固定,且有自己的一套显示效果,那么这里完全无需TabHost布局。
另外一种:
继承自FragmentActivity + TabHost 布局 + ViewPager布局 + Fragment 内容
1:使用findViewById 获得TabHost 对象
2:使用setup()初始化
3:使用TabHost.addTab(tabSpec.setContent(DummyTabFactory));添加Tab标签与空内容
4:在TabHost.onTabChanged中控制ViewPager的实际显示
5:滑动时在ViewPager.onPageSelected 中控制Tab标签的选择。
测试了一下,以FragmentActivity + FragmentTabHost 布局 + Fragment 内容来简单介绍,直接上代码:
主activity_main.xml:<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 这个布局决定了标签在上面还是在下面显示 -->
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
MainActivity:
private FragmentTabHost mTabHost = null;
private View indicator = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
// 添加tab名称和图标
indicator = getIndicatorView("我的联系人", R.layout.mycontact_indicator);
mTabHost.addTab(mTabHost.newTabSpec("myContact")
.setIndicator(indicator), FirstFragment.class, null);
// 添加tab名称和图标
indicator = getIndicatorView("通讯录", R.layout.mycontact_indicator);
mTabHost.addTab(mTabHost.newTabSpec("tongxun").setIndicator(indicator),
SecondFragment.class, null);
// 添加tab名称和图标
indicator = getIndicatorView("特别关心", R.layout.mycontact_indicator);
mTabHost.addTab(mTabHost.newTabSpec("care").setIndicator(indicator),
ThirthFragment.class, null);
}
private View getIndicatorView(String name, int layoutId) {
View v = getLayoutInflater().inflate(layoutId, null);
TextView tv = (TextView) v.findViewById(R.id.tabText);
tv.setText(name);
return v;
}
@Override
protected void onDestroy() {
super.onDestroy();
mTabHost=null;
}
mycontact_indicator.xml:
<?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"
android:gravity="center">
<TextView
android:id="@+id/tabText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:textColor="@drawable/selector_button_text"
android:drawableTop="@drawable/selector_button"
/>
</LinearLayout>
FirstFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView tv=new TextView(getActivity());
tv.setText("第一个fragment");
return tv;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
selector_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@drawable/activity_home_down"></item>
<item android:state_selected="true" android:drawable="@drawable/activity_home_down"></item>
<item android:drawable="@drawable/activity_home"></item>
</selector>
selector_button_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:color="#f60"></item>
<item android:state_selected="true" android:color="#f60"></item>
<item android:color="#000"></item>
</selector>
结果如图所示: