FragmentTabHost实现底部的菜单
实现底部的菜单 有很多种方式,一开始是用TabHost+Activity 后面 又 转为了RadioButton+Fragment,但是都不是特别好,现在一般是FragmentTabHost+Fragment实现的底部菜单布局,从而实现市场上的app的底部菜单的布局等。
FragmentTabHost 实际上是每一个TabSpec组成,指示器为Indicator,而每一个Indicator就是一个View,一般View有ImageView+TextView,具体看需求。当然也可以用LinearLayout添加底部菜单实现。
实现为
将Activity extends FragmentActivity ,从而调用setup()和添加TabSpec
public class MainTabHost extends FragmentActivity {
private FragmentTabHost mTabHost;
private LayoutInflater layoutInflater;
private String mMenuText[] = new String[]{"疯抢","组团","时尚","我的"};
private int mMenuId[] = new int[]{R.mipmap.menu_buy,R.mipmap.menu_shop,R.mipmap.menu_message,R.mipmap.menu_mine};
private Class mMenuFragment[]=new Class[]{BuyFragment.class,TuanFragment.class, MessageFragment.class,MineFragment.class};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_tab_host);
layoutInflater = LayoutInflater.from(this);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this,getSupportFragmentManager(),R.id.tabhostcontent);
for(int i=0;i<mMenuText.length;i++){
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mMenuText[i]).setIndicator(getMenuView(i));
mTabHost.addTab(tabSpec,mMenuFragment[i],null);
}
}
private View getMenuView(int i) {
View view = layoutInflater.inflate(R.layout.item_activity_tab_host,null);
ImageView imageView = (ImageView) view.findViewById(R.id.tabhostimageview);
TextView textView = (TextView) view.findViewById(R.id.tabhosttextview);
imageView.setImageResource(mMenuId[i]);
textView.setText(mMenuText[i]);
return view;
}
}