android 修改 tabLayout 里的字体

最近开发的一款产品,不同的内容、模块使用了很多种字体。我在application里面设置了一种全局的字体,在普通的textView都已生效,但是在tabLayout里面却未生效,以下方法可以修改tabLayout 里面的字体:

//更改字体
public void changeTabsFont(XTabLayout tabLayout) {
    ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
    int tabsCount = vg.getChildCount();
    for (int j = 0; j < tabsCount; j++) {
        ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
        int tabChildsCount = vgTab.getChildCount();
        for (int i = 0; i < tabChildsCount; i++) {
            View tabViewChild = vgTab.getChildAt(i);
            if (tabViewChild instanceof TextView) {
                ((TextView) tabViewChild).setTypeface(TypefaceCache.getInstance(mContext).get("HarmonyOS_Sans_SC_Regular.ttf"));
            }
        }
    }
}

以下是我自己的TypefaceCache类,可直接使用:

public class TypefaceCache {
    private static TypefaceCache sInstance;

    private final Hashtable<String, Typeface> mCache = new Hashtable<String, Typeface>();

    private final Application mApplication;

    private TypefaceCache(Application application) {
        mApplication = application;
    }

    /**
     * If the cache has an instance for the typeface name, this will return the instance immediately.
     * Otherwise this method will create typeface instance and put it into the cache and return the instance.
     *
     * @param name the typeface name.
     * @return {@link Typeface} instance.
     */
    public synchronized Typeface get(String name) {
        Typeface typeface = mCache.get(name);
        if (typeface == null) {
            try {
                typeface = Typeface.createFromAsset(mApplication.getAssets(), name);
            } catch (Exception exp) {
                return null;
            }
            mCache.put(name, typeface);
        }
        return typeface;
    }

    /**
     * Retrieve this cache.
     *
     * @param context the context.
     * @return the cache instance.
     */
    public static synchronized TypefaceCache getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new TypefaceCache((Application) context.getApplicationContext());
        }
        return sInstance;
    }
}

PS:使用此方法,在初始化的时候的确可以达到修改字体的目的,但是在切换viewPager的时候又恢复到了默认的字体,所以我又在在viewPager的addOnPageChangeListener()里面又调用了一次,每次切换的时候重新设置,就解决了此问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android TabLayout上设置字体,你可以使用自定义的TabLayout.TabCustomView来实现。以下是一个示例代码: 首先,在res/layout目录下创建一个自定义的tab_item.xml文件,用于定义TabLayout中每个标签的样式,可以在此文件中设置字体样式。 ```xml <!-- tab_item.xml --> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tab_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="@color/tab_text_color" android:fontFamily="@font/custom_font" /> ``` 接下来,在你的Activity或Fragment中,使用以下代码来设置TabLayout的标签视图: ```java // 获取TabLayout对象 TabLayout tabLayout = findViewById(R.id.tab_layout); // 添加标签 tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.tab_item)); // 设置标签文本 TextView tabText = tabLayout.getTabAt(0).getCustomView().findViewById(R.id.tab_text); tabText.setText("标签1"); ``` 在上面的代码中,你可以看到通过调用`setCustomView()`方法来设置自定义的布局文件作为标签视图。然后,我们可以通过`getCustomView()`方法获取自定义视图,并使用findViewById()来找到TextView并设置文本。 请注意,你需要将自定义字体文件放在res/font目录下,并将其引用到tab_item.xml中的`android:fontFamily`属性中。 这样就可以在Android TabLayout上设置字体了。你可以根据需要自定义标签视图,并在其中设置不同的字体样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值