2019-12-06

自定义TabLayout头部样式



private void initTab() {

mFragmentList =new ArrayList<>();

    mFragmentList.add(HomeFragment.newInstance());

    mFragmentList.add(HomeFragment.newInstance());

    mFragmentList.add(HomeFragment.newInstance());

    mFragmentList.add(HomeFragment.newInstance());

    Adapter adapter =new Adapter(getSupportFragmentManager());

    mCustomViewPager.setAdapter(adapter);

    tabLayout.setupWithViewPager(mCustomViewPager);

    one =tabLayout.getTabAt(0);

    two =tabLayout.getTabAt(1);

    three =tabLayout.getTabAt(2);

    four =tabLayout.getTabAt(3);

    View view1 = View.inflate(getApplicationContext(), R.layout.tablayout_tabitem_view, null);

    one.setCustomView(view1);

    TextView text = (TextView) view1.findViewById(R.id.tv_title);

    TextView ivDot = (TextView) view1.findViewById(R.id.iv_dot);

    text.setText("购物");

    text.setTextSize(18);

    text.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗

    ivDot.setVisibility(View.VISIBLE);

    View view2 = View.inflate(getApplicationContext(), R.layout.tablayout_tabitem_view, null);

    two.setCustomView(view2);

    TextView text2 = (TextView) view2.findViewById(R.id.tv_title);

    text2.setText("发现");

    View view3 = View.inflate(getApplicationContext(), R.layout.tablayout_tabitem_view, null);

    three.setCustomView(view3);

    TextView text3 = (TextView) view3.findViewById(R.id.tv_title);

    text3.setText("推荐");

    View view4 = View.inflate(getApplicationContext(), R.layout.tablayout_tabitem_view, null);

    four.setCustomView(view4);

    TextView text4 = (TextView) view4.findViewById(R.id.tv_title);

    text4.setText("热点");

    //添加tabLayout选中监听

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

@Override

        public void onTabSelected(TabLayout.Tab tab) {

if (tab.getCustomView() !=null) {

tab.getCustomView().findViewById(R.id.iv_dot).setVisibility(View.VISIBLE);

                TextView text = tab.getCustomView().findViewById(R.id.tv_title);

                text.setTextSize(18);

                text.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗

            }

}

@Override

        public void onTabUnselected(TabLayout.Tab tab) {

if (tab.getCustomView() !=null) {

tab.getCustomView().findViewById(R.id.iv_dot).setVisibility(View.GONE);

                TextView text = tab.getCustomView().findViewById(R.id.tv_title);

                text.setTextSize(14);

                text.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));//常规

            }

}

@Override

        public void onTabReselected(TabLayout.Tab tab) {

}

});

    mCustomViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

}

private class Adapterextends FragmentPagerAdapter {

public Adapter(FragmentManager fm) {

super(fm);

    }

@Override

    public FragmentgetItem(int position) {

return mFragmentList.get(position);

    }

@Override

    public int getCount() {

return mFragmentList.size();

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • IDEA常用快捷键 Ctrl + Z 撤回 Ctrl + F 在当前文件查找文本 Ctrl + R 在当前文件替换...
    慈悲渡魂落阅读 185评论 0 0
  • 一,精品微课制作系统 中视天威TV-Class微课程制作系统适合不需要大空间拍摄教学视频的您,只要...
    bjzstw阅读 36评论 0 0
  • 一,精品微课制作系统 中视天威TV-Class微课程制作系统适合不需要大空间拍摄教学视频的您,只要...
    bjzstw阅读 67评论 0 0
  • 1.分布式: 分布式系统就是由多个节点(计算机服务器)组成的系统。而且这些节点一般不是孤立的,是互通的。这些联通的...
    蘇洋阅读 158评论 0 0
  • 明圆的月亮是透过云层悄悄看你的佳人,密布的云层是遮盖佳人容颜的面纱。陡峭的山峰是守护着佳人的...
    浊了眼眸的雾阅读 193评论 0 0