如何更改PagerTabStrip的字体

4
我是使用带有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>

根据这个链接,我找到了一个可能解决我的问题的解决方案。

获取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)
}
}

But我不明白它的意思。
我的布局是:

Image

我想将标题标签的字体样式更改为其他样式,如下所示:

Image


尝试使用我在答案中解释的 android:textAppearance 属性...!! - Vishesh Chandra
3个回答

6
您需要做的是为文本创建样式,然后使用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种默认字体可用,我想要类似于这样的东西:<item name="android:typeface">@assets/font1.ttf</item>。 - Emad
1
然后您可以使用Java Reflection API访问非公开的title textView,并通过创建android.support.v4.view.PagerTabStrip的子类来更新textStyle。 - Vishesh Chandra
你能再解释一下吗? :) - Emad
如何为PagerTabStrip文本视图设置字体类型? - Emad
1
这是PagerTabStrip的源代码:https://chromium.googlesource.com/android_tools/+/881586ca84f2fb8e82faa9c8d645416d175d0f01/sdk/extras/android/support/v4/src/java/android/support/v4/view/PagerTitleStrip.java,反射API的示例为:https://dev59.com/52Yr5IYBdhLWcg3w6OGd#26263487。 - Vishesh Chandra
谢谢您的及时回复 :) 我认为我应该学习更多关于反射的知识... 谢谢兄弟 ;-) - Emad

4
你可以将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,问题出在哪里? - Emad
你是在静态方法中使用这个东西吗? - Ashish Agrawal
在我的应用程序中,我有一个名为MainActivity的活动,其中包含具有PagerTabStrip的ViewPager...所以我有:MainActivity- PagerAdapterfrgmentA,我应该在哪里使用您的代码?;) 当然,我在所有三个地方都使用了它们...但是出现了错误! - Emad
你是通过 findViewById 获取 PagerTabStrip 实例吗? - Ashish Agrawal
PagerTabStrip - FragmentA - ActivityMain - layout -- 感谢您的时间 :) - Emad
在上面的评论中,有一个打字错误:D PagerTabStrip ==> PagerAdapter - Emad

1
将.ttf文件放入assets文件夹中,并在onCreate方法中使用以下代码。
fontTypeFace= Typeface.createFromAsset(getAssets(),
            "fontawesome-webfont.ttf");

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

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