如何在FragmentTabHost添加tab后获取Fragment实例
1个回答
展开全部
//定义FragmentTabHost对象
private MyFragmentTabHost mTabHost;
//定义数组来存放Fragment界面
private Class fragmentArray[] = {BaseFragment.class,BaseFragment.class,BaseFragment.class,BaseFragment.class,BaseFragment.class};
//Tab选项卡的文字
private String mTextviewArray[] = {"a", "b", "c", "d", "e"};
//实例化TabHost对象,得到TabHost
mTabHost = (SunnyFragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//得到fragment的个数
int count = fragmentArray.length;
for(int i = 0; i < count; i++){
//为每一个Tab按钮设置图标、文字和内容
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
//将Tab按钮添加进Tab选项卡中
mTabHost.addTab(tabSpec, fragmentArray[i], null);
//设置Fragment的背景
BaseFragment bf = (BaseFragment)getSupportFragmentManager().findFragmentByTag(mTextviewArray[i]);
if(bf!=null)
bf.setBackgroundResource(mBgImgs[i]);
private MyFragmentTabHost mTabHost;
//定义数组来存放Fragment界面
private Class fragmentArray[] = {BaseFragment.class,BaseFragment.class,BaseFragment.class,BaseFragment.class,BaseFragment.class};
//Tab选项卡的文字
private String mTextviewArray[] = {"a", "b", "c", "d", "e"};
//实例化TabHost对象,得到TabHost
mTabHost = (SunnyFragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//得到fragment的个数
int count = fragmentArray.length;
for(int i = 0; i < count; i++){
//为每一个Tab按钮设置图标、文字和内容
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
//将Tab按钮添加进Tab选项卡中
mTabHost.addTab(tabSpec, fragmentArray[i], null);
//设置Fragment的背景
BaseFragment bf = (BaseFragment)getSupportFragmentManager().findFragmentByTag(mTextviewArray[i]);
if(bf!=null)
bf.setBackgroundResource(mBgImgs[i]);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询