-
首先使用FragmentTabhost的Activity必须继承自FragmentActivity
public class MainActivity extends FragmentActivity
-
调用setup方法进行FragmentManager和用来显示fragment的FrameLayout的绑定。
mTabHost.setup(this, getSupportFragmentManager(), R.id.main_frame);
-
调用newTabSpec创建tab选项
TabSpec tabSpec = mTabHost.newTabSpec(getString(mTitleID[i])) .setIndicator(getItemView(i));
-
调用addTab进行tab选项和fragment类进行绑定。
mTabHost.addTab(tabSpec,mFragmentsClass[i], null);
-
调用mTabHost.getTabWidget().setDividerDrawable(null)去除每个tab之间的分割线。
mTabHost.getTabWidget().setDividerDrawable(null);
-
FragmentTabhost的优化:(防止FragmentTabhost切换fragment时不断销毁和创建view)。
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub if (mCacheView == null) { Log.i("Test", "FindFragment onCreateView"); mCacheView = inflater.inflate(R.layout.fragment_find, null); } ViewGroup parent = (ViewGroup) mCacheView.getParent(); if (parent != null) { parent.removeView(mCacheView); } return mCacheView; }
-
详细代码。
-
MainActivity.java
package com.example.fragmenttabhost; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.TabHost.TabSpec; import android.widget.TextView; public class MainActivity extends FragmentActivity { private FragmentTabHost mTabHost; private Class mFragmentsClass[] = { NewsFragment.class, RecreationFragment.class, MediaFragment.class, FindFragment.class }; private int mTitleID[] = { R.string.news, R.string.recreation, R.string.meidia, R.string.find }; private int mImageID[] = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher }; private final int mItemCount = 4; private LayoutInflater mInflater; @Override protected void onCreate(Bundle arg0) { // TODO Auto-generated method stub super.onCreate(arg0); setContentView(R.layout.activity_main); initView(); } private void initView() { mInflater = LayoutInflater.from(this); mTabHost = (FragmentTabHost) findViewById(R.id.main_tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.main_frame); for (int i = 0; i < mItemCount; i++) { TabSpec tabSpec = mTabHost.newTabSpec(getString(mTitleID[i])) .setIndicator(getItemView(i)); mTabHost.addTab(tabSpec,mFragmentsClass[i], null); } mTabHost.getTabWidget().setDividerDrawable(null); } //单个tab选项卡的视图获取。 private View getItemView(int index) { View v = mInflater.inflate(R.layout.view_tab_item, null); ImageView imageView = (ImageView) v.findViewById(R.id.tab_item_image); imageView.setBackgroundResource(mImageID[index]); TextView textView = (TextView) v.findViewById(R.id.tab_item_text); textView.setText(getString(mTitleID[index])); return v; } }
-
FindFragment.java(其他的fragment和这个类似就不贴代码了)
package com.example.fragmenttabhost; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FindFragment extends Fragment{ private View mCacheView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub if (mCacheView == null) { Log.i("Test", "FindFragment onCreateView"); mCacheView = inflater.inflate(R.layout.fragment_find, null); } ViewGroup parent = (ViewGroup) mCacheView.getParent(); if (parent != null) { parent.removeView(mCacheView); } return mCacheView; } }
-
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/main_frame" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:layout_width="match_parent" android:layout_height="1px" android:background="#c7c7c7" /> <android.support.v4.app.FragmentTabHost android:id="@+id/main_tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:background="#f4f4f4" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>
-
fragment_find.xml(其他的fragment和这个类似就不贴代码了)
<?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:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/find_tips" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/find" /> </LinearLayout>
-
view_tab_item.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:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/tab_item_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tab_item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
-
效果图: