FragmentTabHost的使用

步骤:

1、编写xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:background="@color/bg_color"
        />


    <hzu.calculator.net.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" />
    </hzu.calculator.net.widget.FragmentTabHost>


</LinearLayout>
2、找到控件id

mTabhost = (FragmentTabHost) this.findViewById(android.R.id.tabhost);
mTabhost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);
3、设置添加TabSpec

TabHost.TabSpec tabSpec = mTabhost.newTabSpec(getString(tab.getTitle()));
tabSpec.setIndicator(buildIndicator(tab));
mTabhost.addTab(tabSpec,tab.getFragment(),null);


添加多个tab可以将tab放置在一个集合中,添加tab时遍历出来

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

    mTabs.add(tab_home);
    mTabs.add(tab_hot);
    mTabs.add(tab_category);
    mTabs.add(tab_cart);
    mTabs.add(tab_mine);



    mInflater = LayoutInflater.from(this);
    mTabhost = (FragmentTabHost) this.findViewById(android.R.id.tabhost);
    mTabhost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);

    for (Tab tab : mTabs){

        TabHost.TabSpec tabSpec = mTabhost.newTabSpec(getString(tab.getTitle()));

        tabSpec.setIndicator(buildIndicator(tab));

        mTabhost.addTab(tabSpec,tab.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;
}
tab类文件:

public class Tab {

    private  int title;
    private  int icon;
    private Class fragment;

    public Tab(Class fragment,int title, int icon) {
        this.title = title;
        this.icon = icon;
        this.fragment = fragment;
    }

    public int getTitle() {
        return title;
    }

    public void setTitle(int title) {
        this.title = title;
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public Class getFragment() {
        return fragment;
    }

    public void setFragment(Class fragment) {
        this.fragment = fragment;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值