Android——TabLayout设置选中字体变大,加粗,透明度

TabLayout自带没有设置选中时字体大小的属性,网上搜了好多基本都不生效,搞了一上午终于好使了,记个笔记记录下,布局要自己定义下,要不不好使。

布局中使用TabLayout的xml文件

 

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tablayout"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_alignParentTop="true"
    android:layout_toStartOf="@id/back_container"
    android:layout_toEndOf="@id/mine_container"
    app:tabGravity="fill"
    app:tabIndicatorColor="#ffffff"
    app:tabMode="fixed"
    app:tabIndicatorHeight="2dp"
    app:tabIndicatorFullWidth="false"
    app:tabTextColor="#ffffff" />

java文件中关键代码,直接看addOnTabSelectedListener就行。

 

public TextView toMyTextView;
public TextView toBeReceivedTextView;

public void initView(View v) {
        //此处省略一万行
        tablayout.setupWithViewPager(mPager);
        tablayout.getTabAt(0).setCustomView(R.layout.main_top_item);
        toMyTextView = tablayout.getTabAt(0).getCustomView().findViewById(R.id.tv_top_item);
        tablayout.getTabAt(1).setCustomView(R.layout.main_top_item);
        toBeReceivedTextView = tablayout.getTabAt(1).getCustomView().findViewById(R.id.tv_top_item);
        tablayout.setTabRippleColor(ColorStateList.valueOf(getContext().getResources().getColor(R.color.transparent)));/*去除tablayout 子tab点击时的黑色背景*/
    
    
        //默认选择第一个tab,设置字体大小和默认风格为加粗 toMyTextView是我自己项目中第一个Tab的TextView,自己看着改。
        toMyTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        toMyTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
    
    
    
        //看这里看这里看这里
        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.tv_top_item).setSelected(true);
                TextView tv = tab.getCustomView().findViewById(R.id.tv_top_item);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);//直接用setTextSize(22)也一样
                tv.setAlpha(0.9f);//透明度
                tv.invalidate();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.tv_top_item).setSelected(false);
                TextView tv = tab.getCustomView().findViewById(R.id.tv_top_item);
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                tv.setAlpha(0.6f);
                tv.invalidate();
            }

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

            }
        });
    }

自定义布局main_top_item.xml

 

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_top_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:singleLine="true"
        android:textSize="@dimen/txtsize18sp"
        android:textColor="@color/white"
        android:gravity="center"/>
</RelativeLayout>

验证OK,撒花撒花

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现 Android TabLayout 选中加粗的效果,你可以通过创建一个自定义的 `TabLayout.OnTabSelectedListener` 来实现。 在自定义的 `OnTabSelectedListener` 中,你可以通过设置 `setTypeface` 和 `setFakeBoldText` 方法来实现选中的 Tab 文字加粗的效果。 以下是一个示例代码: ```java TabLayout tabLayout = findViewById(R.id.tab_layout); tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { //设置选中时文字的样式 TextView textView = (TextView)tab.getCustomView(); textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); textView.setFakeBoldText(true); } @Override public void onTabUnselected(TabLayout.Tab tab) { //设置选中时文字的样式 TextView textView = (TextView)tab.getCustomView(); textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL)); textView.setFakeBoldText(false); } @Override public void onTabReselected(TabLayout.Tab tab) { } }); ``` 在上面的示例代码中,`addTabSelectedListener` 方法用于添加自定义的 `OnTabSelectedListener`,其中 `onTabSelected` 方法用于设置选中的 Tab 文字样式,`onTabUnselected` 方法用于设置选中的 Tab 文字样式。 在 `onTabSelected` 方法中,我们通过 `getTypeface` 方法获取当前选中的 Tab 的字体样式,然后使用 `Typeface.defaultFromStyle` 方法创建一个加粗字体样式,接着使用 `setFakeBoldText` 方法将选中的 Tab 的文字加粗。 在 `onTabUnselected` 方法中,我们只需要恢复未选中的 Tab 的字体样式即可。 需要注意的是,在设置 Tab 的自定义视图时,你需要使用 `setCustomView` 方法将自定义的视图设置给 Tab,如下所示: ```java for (int i = 0; i < tabLayout.getTabCount(); i++) { TabLayout.Tab tab = tabLayout.getTabAt(i); TextView textView = new TextView(this); textView.setText("Tab " + (i + 1)); textView.setGravity(Gravity.CENTER); tab.setCustomView(textView); } ``` 上面的代码中,我们使用 `TextView` 创建了一个自定义的视图,然后使用 `setCustomView` 方法将自定义的视图设置给 Tab。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值