如何使用自定义字体/书法更改tabLayout的字体

6
我正在寻找有关如何更改TabLayout中这些选项卡字体为自定义字体的答案。
我尝试了以下方法,但没有起作用:
 Typeface hero = Typeface.createFromAsset(getContext().getAssets(),"fonts/Hero Light.otf");

            textViewToConvert.setTypeface(hero);
        }
    }

请解释一下"它没用了"是什么意思。尝试从文件名中删除空格。您可以查看其他选项卡实现,这些实现可能提供内置字体配置。 - CommonsWare
5个回答

25

在您的布局中

<android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        style="@style/Tab"
        android:layout_width="match_parent"
        android:layout_height="54dp"
        app:tabTextAppearance="@style/MineCustomTabText"/>

并且在你的样式文件夹中

<style name="MineCustomTabText" parent="TextAppearance.Design.Tab">
    <item name="android:textSize">12sp</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:fontFamily">@font/two_light_1</item>
</style>

4
我们通过样式来实现。首先定义标签中的文本具有特殊的样式:
<android.support.design.widget.TabLayout
app:tabTextAppearance="@style/transactions__month_tabs_text_appearance">

然后在样式中定义您想要使用的自定义字体:

  <style name="transactions__month_tabs_text_appearance" parent="TextAppearance.Design.Tab">
    <item name="fontPath">fonts/your-font-name.ttf</item>
  </style>

顺便提一下,我们将所有字体名称放到字符串资源中。


它没有工作 :/ <style name="transactions__month_tabs_text_appearance" parent="TextAppearance.Design.Tab"> <item name="fontPath">fonts/Gidole-Regular.otf</item> </style> - Alpoe
这是我的 XML 代码: - Alpoe
字体文件是否在“assets”文件夹中的“font”目录下?你的活动中是否有其他使用自定义字体的文本,并且它们能正常工作? - Eugen Martynov
是的,我的字体位于assets/fonts/...。 我的活动中还有其他TextView,我使用了Calligraphy库应用了自定义字体。 - Alpoe
是的,没错。那么我就不知道问题可能出在哪里了。 - Eugen Martynov
显示剩余6条评论

2

您可以在Xml布局中的TabLayout中使用tabTextAppearance。


2
我在我的项目中使用这种方法,效果非常好,在应用程序的不同位置,如果需要,我可以定制选项卡字体和背景颜色,这种方式非常高效。
public static void changeTabsFont(Context context, TabLayout tabLayout, int color) {

    ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
    tabLayout.setBackgroundColor(color);
    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(Typeface.createFromAsset(context.getAssets(), "Lato-Regular.ttf"));
            }
        }
    }
}

1
从Java代码或XML中创建TextView,可以像这样(确保保留该id)。
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:textSize="15sp"
android:textColor="@color/tabs_default_color"
android:gravity="center"
android:layout_height="match_parent"
/>

然后从代码中。
for (int i = 0; i < tabLayout.getTabCount(); i++) {
    //R.layout is the previous defined xml
 TextView tv=(TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null)
 tv.setTypeface(Typeface);       
 tabLayout.getTabAt(i).setCustomView(tv);

}

我在另一个答案上看到过这个,并且之前也尝试过,但是效果不太好。 - Alpoe
它没有崩溃,只是看起来一样。字体没有改变。 - Alpoe
你确定你应用的字体不是默认字体吗? - Martin De Simone
如果您没有更改XML的ID,则字体可能存在问题,请尝试使用新字体以查看是否有效。 - Martin De Simone
好的,我再试一次。现在它说“无法应用布局充气机”,顺便说一下,我是从一个叫做主片段的类中进行操作的。 - Alpoe
你导入了吗? - Martin De Simone

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接