我使用的是带有ViewPager的PagerTabStrip,我需要设置自定义字体(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)
}
}但我不明白这意味着什么。
我的布局是:

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

发布于 2015-09-21 07:11:39
您需要为文本创建一个样式,然后使用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-21 09:33:38
您可以将font.ttf放在资产/字体/文件夹中,并获取字体实例
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)
}
}发布于 2016-07-13 10:25:00
将.ttf文件放入资产文件夹,并在onCreate方法中使用此代码
fontTypeFace= Typeface.createFromAsset(getAssets(),
"fontawesome-webfont.ttf");
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setTypeface(fontTypeFace,0);https://stackoverflow.com/questions/32689103
复制相似问题