自定义TabLayout(可调整背景布局、字体颜色及大小)

记录一个简单的自定义TabLayout,可自定义tab背景,tab选中字体颜色、大小。

TabLayout代码:

public class HomeTabLayout extends TabLayout {

    public HomeTabLayout(Context context) {
        super(context);
        initTabLayout();
    }

    public HomeTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initTabLayout();
    }

    public HomeTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTabLayout();
    }

    private void initTabLayout() {

        this.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(Tab tab) {

                if (tab.getCustomView() != null) {
                    TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
                    ImageView tab_iv = tab.getCustomView().findViewById(R.id.tab_iv);
                    tab_text.setTextColor(UIHelper.getColor(R.color.text_color_1));
                    tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
                    tab_text.setVisibility(VISIBLE);
                    tab_iv.setVisibility(VISIBLE);
                }
            }

            @Override
            public void onTabUnselected(Tab tab) {

                if (tab.getCustomView() != null) {
                    TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
                    ImageView tab_iv = tab.getCustomView().findViewById(R.id.tab_iv);
                    tab_text.setTextColor(UIHelper.getColor(R.color.text_color_2));
                    tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                    tab_text.setVisibility(VISIBLE);
                    tab_iv.setVisibility(INVISIBLE);
                }
            }

            @Override
            public void onTabReselected(Tab tab) {

            }
        });
    }

    public void bindTitle(List<String> titles) {

        for (int i = 0, size = titles.size(); i < size; i++) {
            Tab tab = getTabAt(i);
            tab.setCustomView(R.layout.hometablayout_item);
            TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
            tab_text.setTextColor(UIHelper.getColor(R.color.text_color_2));
            tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            tab_text.setText(titles.get(i));

            //设置选中
            if (i == 1)
                tab.select();
//            if (i == 1)
//                this.addTab(tab, true);
//            else
//                this.addTab(tab, false);
        }

    }
}


布局中使用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.***********.myapplication.HomeTabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="#FFFFFF"
        app:tabBackground="@color/transparency"
        app:tabIndicatorHeight="0dp"
        app:tabMode="scrollable"
        app:tabRippleColor="@color/transparency"
        app:tabSelectedTextColor="@color/text_color_1"
        app:tabTextColor="@color/text_color_2" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
MainActivity 中操作:
public class MainActivity extends AppCompatActivity {

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

        HomeTabLayout tabLayout = findViewById(R.id.tab_layout);
        ViewPager viewPager = findViewById(R.id.viewPager);

        List<String> mTilte = new ArrayList<>();
        List<Fragment> fragments = new ArrayList<>();
        FragmentOne fragmentOne = new FragmentOne();
        mTilte.add(UIHelper.getString(R.string.home_tab_one));
        fragments.add(fragmentOne);
        FragmentTwo fragmentTwo = new FragmentTwo();
        mTilte.add(UIHelper.getString(R.string.home_tab_two));
        fragments.add(fragmentTwo);
        FragmentThree fragmentThree = new FragmentThree();
        mTilte.add(UIHelper.getString(R.string.home_tab_three));
        fragments.add(fragmentThree);

        TabPagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(4);
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.bindTitle(mTilte);
    }

    class TabPagerAdapter extends FragmentPagerAdapter {

        List<Fragment> fragments;

        TabPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

    }

}

UIHelper为简单的工具类。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值