【安卓】安卓App开发思路 一步一个脚印(二)FragmentTabHost实现底部的菜单

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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值