TabLayout 设置选中Tab字体加粗

通过监听 Tab 卡片的切换 + SpannableString 富文本的方式。

来看下具体的代码吧~

// tabLayout的滑动选择监听
        binding.previewTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab == null || tab.getText() == null) {
                    return;
                }
                // 选中字体加粗
                String trim = tab.getText().toString().trim();
                SpannableString spStr = new SpannableString(trim);
                StyleSpan styleSpan_B = new StyleSpan(Typeface.BOLD);
                spStr.setSpan(styleSpan_B, 0, trim.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                tab.setText(spStr);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab == null || tab.getText() == null) {
                    return;
                }
                // 选中字体不加粗
                String trim = tab.getText().toString().trim();
                SpannableString spStr = new SpannableString(trim);
                StyleSpan styleSpan_B = new StyleSpan(Typeface.NORMAL);
                spStr.setSpan(styleSpan_B, 0, trim.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                tab.setText(spStr);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以通过在TabLayout中设置Tab的自定义View来实现选中大小的效果。具体做法如下: 1. 创建一个自定义的TabLayout.TabView,例如: ``` public class CustomTabView extends LinearLayout implements TabLayout.OnTabSelectedListener { private TextView mTabText; private ImageView mTabImage; public CustomTabView(Context context) { super(context); initView(); } public CustomTabView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } private void initView() { View view = LayoutInflater.from(getContext()).inflate(R.layout.custom_tab_view, this); mTabText = view.findViewById(R.id.tab_text); mTabImage = view.findViewById(R.id.tab_image); } @Override public void onTabSelected(TabLayout.Tab tab) { mTabText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); mTabImage.setScaleX(1.2f); mTabImage.setScaleY(1.2f); } @Override public void onTabUnselected(TabLayout.Tab tab) { mTabText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); mTabImage.setScaleX(1.0f); mTabImage.setScaleY(1.0f); } @Override public void onTabReselected(TabLayout.Tab tab) { } } ``` 2. 在TabLayout中设置自定义的TabView,例如: ``` TabLayout tabLayout = findViewById(R.id.tab_layout); tabLayout.addTab(tabLayout.newTab().setCustomView(new CustomTabView(this))); ``` 通过在CustomTabView中实现TabLayout.OnTabSelectedListener接口,可以在选中和未选中状态下分别设置Tab的大小。 以上就是设置TabLayout选中大小的方法,希望能对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值