步骤:
1、编写xml文件
<?xml version="1.0" encoding="utf-8"?> <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" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".MainActivity" android:orientation="vertical">
<FrameLayout android:id="@+id/realtabcontent" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:background="@color/bg_color" /> <hzu.calculator.net.widget.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/white" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </hzu.calculator.net.widget.FragmentTabHost> </LinearLayout>2、找到控件id
mTabhost = (FragmentTabHost) this.findViewById(android.R.id.tabhost); mTabhost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);3、设置添加TabSpec
TabHost.TabSpec tabSpec = mTabhost.newTabSpec(getString(tab.getTitle())); tabSpec.setIndicator(buildIndicator(tab)); mTabhost.addTab(tabSpec,tab.getFragment(),null);
private void initTab() { Tab tab_home = new Tab(HomeFragment.class,R.string.home,R.drawable.selector_icon_home); Tab tab_hot = new Tab(HotFragment.class,R.string.hot,R.drawable.selector_icon_hot); Tab tab_category = new Tab(CategoryFragment.class,R.string.catagory,R.drawable.selector_icon_category); Tab tab_cart = new Tab(CartFragment.class,R.string.cart,R.drawable.selector_icon_cart); Tab tab_mine = new Tab(MineFragment.class,R.string.mine,R.drawable.selector_icon_mine); mTabs.add(tab_home); mTabs.add(tab_hot); mTabs.add(tab_category); mTabs.add(tab_cart); mTabs.add(tab_mine); mInflater = LayoutInflater.from(this); mTabhost = (FragmentTabHost) this.findViewById(android.R.id.tabhost); mTabhost.setup(this,getSupportFragmentManager(),R.id.realtabcontent); for (Tab tab : mTabs){ TabHost.TabSpec tabSpec = mTabhost.newTabSpec(getString(tab.getTitle())); tabSpec.setIndicator(buildIndicator(tab)); mTabhost.addTab(tabSpec,tab.getFragment(),null); } mTabhost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE); mTabhost.setCurrentTab(0); } private View buildIndicator(Tab tab){ View view =mInflater.inflate(R.layout.tab_indicator,null); ImageView img = (ImageView) view.findViewById(R.id.icon_tab); TextView text = (TextView) view.findViewById(R.id.txt_indicator); img.setBackgroundResource(tab.getIcon()); text.setText(tab.getTitle()); return view; }tab类文件:
public class Tab { private int title; private int icon; private Class fragment; public Tab(Class fragment,int title, int icon) { this.title = title; this.icon = icon; this.fragment = fragment; } public int getTitle() { return title; } public void setTitle(int title) { this.title = title; } public int getIcon() { return icon; } public void setIcon(int icon) { this.icon = icon; } public Class getFragment() { return fragment; } public void setFragment(Class fragment) { this.fragment = fragment; } }