如何改变PagerTabStrip的字体

问题描述 投票:2回答:3

我正在使用带有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)
}
}

但我不明白这意味着什么。

我的布局是:

Image

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

Image

android android-viewpager android-tabstrip
3个回答
4
投票

你需要做的是为文本创建一个样式,然后使用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" />

3
投票

您可以将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)
    }
}

1
投票

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

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

 tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setTypeface(fontTypeFace,0);
© www.soinside.com 2019 - 2024. All rights reserved.