首页 > 代码库 > 如何 TabHostFragment自定义 tab键(indicator)

如何 TabHostFragment自定义 tab键(indicator)

1 获得 tabHostFragment:

ActionBarActivity activity2 = (ActionBarActivity) activity;

mTabHost = new FragmentTabHost(activity2);

mTabHost.setup(getActivity(),getChildFragmentManager(),R.id.content_frame);

2 加入系统默认的indicator

mTabHost.addTab(mTabHost.newTabSpec("tour").setIndicator("巡视"),TourFragmentTab.class,null);

3 加入自定义的indicator

indicator = getIndicatorView("任务", R.layout.tab_custom);

mTabHost.addTab(mTabHost.newTabSpec("task").setIndicator(indicator),TaskFragment.class,null);

4 上面用到的getIndicatorView方法如下:

 private View indicator = null;

public View getIndicatorView(String name,int layoutId) {  

        View v = getActivity().getLayoutInflater().inflate(layoutId,null);  

        TextView tv = (TextView) v.findViewById(R.id.tabText);  

        tv.setText(name);  

        return v;  

    }