FragmentTabHost的使用

1、创建多个Fragment
2、Activity的layout布局:
android.support.v4.app.FragmentTabHost


(id要写@android:id/tabhost)
在Activity中引用
private FragmentTabHost mTabhost;
3、创建Tab类
(成员变量:
    private int title;
    private int image;
    private Class fragment

;)
4、创建Tab集合,添加Tab对象(List<Tab>)
5、调用
mTabhost.setup(MainActivity.this,getSupportFragmentManager(),R.id.frame)


(第三个参数是fragment在本Activity中的位置)
6、for循环添加
for (Tab tab:mTabs){
            TabHost.TabSpec tabSpec = mTabhost.newTabSpec(getString(tab.getTitle()));
            tabSpec.setIndicator(bindIndicator(tab));
            mTabhost.addTab(tabSpec,tab.getFragment(),null);
        }


7、设置默认页
mTabhost.setCurrentTab(0);


8、创建指示器方法(全局)
private View bindIndicator(Tab tab) {
        View indicatorView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_indicator, null);
        ImageView iv = indicatorView.findViewById(R.id.iv_tab_image);
        TextView tv = indicatorView.findViewById(R.id.tv_tab_title);
        
        iv.setImageResource(tab.getImage());
        tv.setText(tab.getTitle());
        return indicatorView;
    }


9、设置图片selector和字体selector
mTabs.add(new Tab(R.string.first,R.drawable.selector_image,FirstFragment.class));
 、android:textColor="@drawable/selector_font"

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值