1、创建多个Fragment
2、Activity的layout布局:
(id要写@android:id/tabhost)
在Activity中引用
private FragmentTabHost mTabhost;
3、创建Tab类
(成员变量:
;)
4、创建Tab集合,添加Tab对象(List<Tab>)
5、调用
(第三个参数是fragment在本Activity中的位置)
6、for循环添加
7、设置默认页
8、创建指示器方法(全局)
9、设置图片selector和字体selector
(
)
2、Activity的layout布局:
android.support.v4.app.FragmentTabHost
(id要写@android:id/tabhost)
在Activity中引用
private FragmentTabHost mTabhost;
3、创建Tab类
(成员变量:
private int title;
private int image;
private Class fragment
;)
4、创建Tab集合,添加Tab对象(List<Tab>)
5、调用
mTabhost.setup(MainActivity.this,getSupportFragmentManager(),R.id.frame)
(第三个参数是fragment在本Activity中的位置)
6、for循环添加
for (Tab tab:mTabs){
TabHost.TabSpec tabSpec = mTabhost.newTabSpec(getString(tab.getTitle()));
tabSpec.setIndicator(bindIndicator(tab));
mTabhost.addTab(tabSpec,tab.getFragment(),null);
}
7、设置默认页
mTabhost.setCurrentTab(0);
8、创建指示器方法(全局)
private View bindIndicator(Tab tab) {
View indicatorView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_indicator, null);
ImageView iv = indicatorView.findViewById(R.id.iv_tab_image);
TextView tv = indicatorView.findViewById(R.id.tv_tab_title);
iv.setImageResource(tab.getImage());
tv.setText(tab.getTitle());
return indicatorView;
}
9、设置图片selector和字体selector
(
mTabs.add(new Tab(R.string.first,R.drawable.selector_image,FirstFragment.class));
、android:textColor="@drawable/selector_font"
)