Android 设置TabLayout选中后的字体、大小、颜色等设置

  1. 初始化
    1)在xml中设置颜色变化
    app:tabTextColor="@color/white_7f"
    app:tabSelectedTextColor="@color/white"
    其中,tabTextColor未未选中时的颜色,tabSelectedTextColor为选中时的颜色。
    2)对已定义好的TabLayout进行处理。
    //获取TabLayout设置的字体颜色,包含tabTextColor及tabSelectedTextColor
    ColorStateList colorStateList = tabLayout.getTabTextColors();
    //对每个Tab 设置customView,设置为TextView,用于设置字体大小等
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        assert tab != null;
        String tabStr = Objects.requireNonNull(tab.getText()).toString();
        if(tab.getCustomView() == null || !(tab.getCustomView() instanceof TextView)){
             TextView tv = new TextView(tabLayout.getContext());
             tv.setTextColor(colorStateList);
             tv.setText(tabStr);
             tv.setTextSize(tab.isSelected()?selectSize:unSelectSize);
             tab.setCustomView(tv);
        }
    }
  2. 在监听器中设置样式
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    String tabStr = Objects.requireNonNull(tab.getText()).toString();
                    if(onSelectedListener!=null){
                        onSelectedListener.onSelected(tabStr);
                    }
                    if(enableChangeSize){
                        TextView tv = (TextView) tab.getCustomView();
                        assert tv != null;
                        tv.setTextSize(selectSize);
                    }
                }
    
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    if(enableChangeSize){
                        TextView tv = (TextView) tab.getCustomView();
                        assert tv != null;
                        tv.setTextSize(unSelectSize);
                    }
                }
    
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
    
                }
            });

    在选中时或未选中时,获取已设置的TextView,然后可以去设置需要的大小、加粗等变化。

  3. 我做了一个简单的封装,这里分享一下。

    
    public class TabLayoutUtil {
        private final TabLayout tabLayout;
        private boolean enableChangeSize = false;
        private int unSelectSize = 15,selectSize = 16;
    
        private TabLayoutUtil(TabLayout tabLayout) {
            this.tabLayout = tabLayout;
        }
    
        public static TabLayoutUtil build(TabLayout tabLayout){
            return new TabLayoutUtil(tabLayout);
        }
    
        public TabLayoutUtil enableChangeStyle() {
            this.enableChangeSize = true;
            ColorStateList colorStateList = tabLayout.getTabTextColors();
            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                TabLayout.Tab tab = tabLayout.getTabAt(i);
                assert tab != null;
                String tabStr = Objects.requireNonNull(tab.getText()).toString();
                if(tab.getCustomView() == null || !(tab.getCustomView() instanceof TextView)){
                    TextView tv = new TextView(tabLayout.getContext());
                    //使用默认TabItem样式时,需要添加LayoutParams,否则会出现Tab文字不居中问题
                    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(-2,-2);
                    tv.setLayoutParams(params);
                    tv.setTextColor(colorStateList);
                    tv.setText(tabStr);
                    tv.setTextSize(tab.isSelected()?selectSize:unSelectSize);
                    tab.setCustomView(tv);
                }
            }
            return this;
        }
    
        public TabLayoutUtil setTextSizes(int selectSize,int unSelectSize) {
            this.selectSize = selectSize;
            this.unSelectSize = unSelectSize;
            return this;
        }
    
    
        public TabLayoutUtil setOnSelectedListener(OnSelectedListener onSelectedListener) {
            tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    String tabStr = Objects.requireNonNull(tab.getText()).toString();
                    if(onSelectedListener!=null){
                        onSelectedListener.onSelected(tabStr);
                    }
                    if(enableChangeSize){
                        TextView tv = (TextView) tab.getCustomView();
                        assert tv != null;
                        tv.setTextSize(selectSize);
                    }
                }
    
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    if(enableChangeSize){
                        TextView tv = (TextView) tab.getCustomView();
                        assert tv != null;
                        tv.setTextSize(unSelectSize);
                    }
                }
    
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
    
                }
            });
            return this;
        }
    
        public interface OnSelectedListener{
            void onSelected(String tabStr);
        }
    }
    

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
要改变TabLayout中选中Tab的字体大小,可以通过自定义TabLayout.Tab的布局来实现。具体步骤如下: 1. 在res/layout目录下新建一个xml文件,例如:custom_tab.xml。 2. 在这个xml文件中定义一个TextView,并设置选中和未选中时的字体大小,例如: ```xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="@color/tab_unselected" android:textAppearance="@style/TextAppearance.Design.Tab" /> ``` 其中,android:id="@android:id/text1"是必须的,因为TabLayout会通过这个id来查找TextView,并设置Tab的文本。 3. 在TabLayout中设置自定义Tab的布局,例如: ```java TabLayout tabLayout = findViewById(R.id.tab_layout); tabLayout.setupWithViewPager(viewPager); for (int i = 0; i < tabLayout.getTabCount(); i++) { TabLayout.Tab tab = tabLayout.getTabAt(i); if (tab != null) { tab.setCustomView(R.layout.custom_tab); } } tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { TextView textView = tab.getCustomView().findViewById(android.R.id.text1); textView.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.tab_selected)); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); } @Override public void onTabUnselected(TabLayout.Tab tab) { TextView textView = tab.getCustomView().findViewById(android.R.id.text1); textView.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.tab_unselected)); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); } @Override public void onTabReselected(TabLayout.Tab tab) { } }); ``` 在设置Tab的布局时,使用tab.setCustomView(R.layout.custom_tab)来设置自定义的布局。 在添加Tab选中监听器时,通过tab.getCustomView().findViewById(android.R.id.text1)来获取TextView,并设置不同状态下的字体大小颜色。例如,在onTabSelected方法中,设置字体大小为20sp,颜色为R.color.tab_selected。 注意:这里的R.color.tab_selected和R.color.tab_unselected是自定义的颜色值,可以根据自己的需求进行设置
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值