用FragmentTabHost 实现底部菜单

FagmentTabHost 类似于 TabHost,但TabHost是为Activity服务的,过于重量级,已经被淘汰;而FragmentTabHost则是为 Fragment 专门服务的。

使用:

①将FragmentTabHost.java文件放在项目中

②布局文件activity_main.xml

<FrameLayout
    android:id="@+id/realtabcontent"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:background="@color/bg_color"
    />


<com.example.administrator.shopdemo.widget.FragmentTabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0" />
</com.example.administrator.shopdemo.widget.FragmentTabHost>

MainActivity继承FragmentActivity,因为AppCompatActivity继承了FragmentActivity,所以可直接继承AppCompatActivity

public class MainActivity extends AppCompatActivity {

    private FragmentTabHost mTabHost;

    private LayoutInflater mInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mInflater = LayoutInflater.from(this);
        //setup方法
        mTabHost.setup(this, getSupportFragmentManager()R.id.realtabcontent);
        //init TabSpec
        initTab();
    }
    private void initTab(){
        Tab tab_home = new Tab(HomeFragment.class,R.string.home,R.drawable.selector_icon_home);
        Tab tab_hot = new Tab(HotFragment.class,R.string.hot,R.drawable.selector_icon_hot);
        Tab tab_category = new Tab(CategoryFragment.class,R.string.catagory,R.drawable.selector_icon_category);
        Tab tab_cart = new Tab(CartFragment.class,R.string.cart,R.drawable.selector_icon_cart);
        Tab tab_mine = new Tab(MineFragment.class,R.string.mine,R.drawable.selector_icon_mine);


        TabHost.TabSpec tabSpec1 = mTabHost.newTabSpec(getString(tab_home.getTitle()));
        tabSpec1.setIndicator(buildIndicator(tab_home));
        mTabHost.addTab(tabSpec1,tab_home.getFragment(),null);

        TabHost.TabSpec tabSpec2 = mTabHost.newTabSpec(getString(tab_hot.getTitle()));
        tabSpec2.setIndicator(buildIndicator(tab_hot));
        mTabHost.addTab(tabSpec2,tab_hot.getFragment(),null);

        TabHost.TabSpec tabSpec3 = mTabHost.newTabSpec(getString(tab_category.getTitle()));
        tabSpec3.setIndicator(buildIndicator(tab_category));
        mTabHost.addTab(tabSpec3,tab_category.getFragment(),null);

        TabHost.TabSpec tabSpec4 = mTabHost.newTabSpec(getString(tab_cart.getTitle()));
        tabSpec4.setIndicator(buildIndicator(tab_cart));
        mTabHost.addTab(tabSpec4,tab_cart.getFragment(),null);

        TabHost.TabSpec tabSpec5 = mTabHost.newTabSpec(getString(tab_mine.getTitle()));
        tabSpec5.setIndicator(buildIndicator(tab_mine));
        mTabHost.addTab(tabSpec5,tab_mine.getFragment(),null);


        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        mTabHost.setCurrentTab(0);
    }

    private View buildIndicator(Tab tab){

        View view =mInflater.inflate(R.layout.tab_indicator,null);
        ImageView img = (ImageView) view.findViewById(R.id.icon_tab);
        TextView text = (TextView) view.findViewById(R.id.txt_indicator);

        img.setBackgroundResource(tab.getIcon());
        text.setText(tab.getTitle());

        return  view;
    }

}

④首先找到FragmentActivity

mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);

⑤然后调用FragmentActivitysetUp方法,将FragmentTabHost上面的FrameLayout布局id传入:

mTabHost.setup(this, getSupportFragmentManager()R.id.realtabcontent);

⑥新建一个beanTab,用来封装每个Tab所需的参数

public class Tab {

    private  int title;
    private  int icon;
    private Class fragment;
。。。
}

 

⑦初始化Tab

首先,实例化一个Tab bean,将所需的参数传入

然后为此Tab设置TabSpec,,

TabHost.TabSpec tabSpec1 = mTabHost.newTabSpec(getString(tab_home.getTitle()));

Indicator来设置它的布局
tabSpec1.setIndicator(buildIndicator(tab_home));

最后将Tab 传入 TabHost 
mTabHost.addTab(tabSpec1,tab_home.getFragment(),null);

⑧如何为TabSpec设置布局?

->首先,写一个布局tab_indicator.xml

<ImageView
    android:id="@+id/icon_tab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

<TextView
    android:id="@+id/txt_indicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/selector_tab_text"
    android:layout_marginTop="2dp" />

 

->为布局设置选择器 selector ,在当前控件处于不同状态时,分别改变背景图片

->在代码中,将Tab的布局文件填充,分别设置后,传入到TabSpecIndicator

private View buildIndicator(Tab tab){
    View view =mInflater.inflate(R.layout.tab_indicator,null);
    ImageView img = (ImageView) view.findViewById(R.id.icon_tab);
    TextView text = (TextView) view.findViewById(R.id.txt_indicator);
    img.setBackgroundResource(tab.getIcon());
    text.setText(tab.getTitle());
    return  view;
}

 

到目前为止,APP的底部菜单就已经实现了。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值