Android

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

2020-05-26  本文已影响0人  索隆的南南鸟

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,撒花撒花。

上一篇下一篇

猜你喜欢

热点阅读