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

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

我的布局是:

图片

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

图片

4

3 回答 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" />
于 2015-09-21T07:11:39.567 回答
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)
    }
}
于 2015-09-21T09:33:38.217 回答
1

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

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

 tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setTypeface(fontTypeFace,0);
于 2016-07-13T10:25:00.117 回答