【问题标题】:How to change the font of PagerTabStrip如何更改 PagerTabStrip 的字体
【发布时间】:2025-12-19 23:30:10
【问题描述】:

我正在使用带有 PagerTabStrip 的 ViewPager,我需要设置自定义字体 (typeface) ,但 PagerTabStrip 没有 .setTypeFace 功能!

这是我的 XML 代码:

 <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewPager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <android.support.v4.view.PagerTabStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pagerTabStrip"
        android:layout_gravity="top"
        android:background="#FF0000"
        android:textColor="#FFFFFF" />

</android.support.v4.view.ViewPager>

根据this link,我找到了可能解决我的问题的解决方案。

获取 PagerTabStrip 的子视图并检查它是否是 TextView 的实例。如果是,请设置字体:

for (int i = 0; i < pagerTabStrip.getChildCount(); ++i) {
View nextChild = pagerTabStrip.getChildAt(i);
if (nextChild instanceof TextView) {
   TextView textViewToConvert = (TextView) nextChild;
   textViewToConvert.setTypeface(PUT_TYPEFACE_HERE)
}
}

但我不明白这是什么意思。

我的布局是:

我想将标题选项卡更改为另一种字体样式,如下所示:

【问题讨论】:

  • 尝试 android:textAppearance 属性,正如我在回答中解释的那样......!!

标签: android android-viewpager android-tabstrip


【解决方案1】:

你需要做的就是为文本创建一个样式,然后使用android:textAppearance属性...

类似这样的:

 <style name="PagerTabStripText">
     <item name="android:textStyle">italic</item>
 </style>

您的 PagerTabStrip XML 将如下所示:

<android.support.v4.view.PagerTabStrip
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/pagerTabStrip"
    android:layout_gravity="top"
    android:background="#FF0000"
    android:textColor="#FFFFFF"
    android:textAppearance="@style/PagerTabStripText" />

【讨论】:

  • 谢谢 Vishesh,但是我需要为我的 pagerTabstrip 使用自定义字体,在我的研究中,没有办法将外部字体添加到 xml 文件中。xml 中只有 3 种默认字体可用——我想要这样的东西:@assets/font1.ttf
  • 然后您可以使用 Java Reflection API 并访问非公共标题 textView 并通过制作 android.support.v4.view.PagerTabStrip 的子类来更新 textStyle
  • 您能再解释一下吗? :)
  • 像这样:link ?
  • 这是 PagerTabStrip 的源代码 chromium.googlesource.com/android_tools/+/…Reflection API 的示例是: *.com/questions/13307758/…
【解决方案2】:

你可以把 font.ttf 放到 assets/fonts/ 文件夹中,获取字体实例

   Typeface fontTypeFace= Typeface.createFromAsset(getContext().getAssets(),
                "fonts/font.ttf");

for (int i = 0; i < pagerTabStrip.getChildCount(); ++i) {
    View nextChild = pagerTabStrip.getChildAt(i);
    if (nextChild instanceof TextView) {
       TextView textViewToConvert = (TextView) nextChild;
       textViewToConvert.setTypeface(fontTypeFace)
    }
}

【讨论】:

  • 当我使用这个代码时: error: non-static method getChildCount() cannot be referenced from a static context 什么问题...?
  • 你是在静态方法中使用这个东西吗?
  • 在我的应用程序中,我有一个活动 MainActivity ,其中包含一个带有 PagerTabStrip 的查看器...所以我有:MainActivity - PagerAdapterfrgmentA ,我应该在哪里使用你的代码? ;) ofcource 我在所有三个中都使用了它们......但是发生了错误!
  • 您是否正在使用 findViewById 获取 pagerTabstrip 实例
  • PagerTabStrip - FragmentA - ActivityMain - layout -- 谢谢你的时间:)
【解决方案3】:

将 .ttf 文件放入 assets 文件夹并在 onCreate 方法中使用此代码

fontTypeFace= Typeface.createFromAsset(getAssets(),
            "fontawesome-webfont.ttf");

 tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setTypeface(fontTypeFace,0);

【讨论】:

    最近更新 更多