如何更改PagerTabStrip的字体
How to change the font of PagerTabStrip
我正在使用带有 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,我找到了可能解决我的问题的方案。
Get the child views of the PagerTabStrip and check if it is an instance of a TextView. If it is, set the typeface:
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属性...
像这样:
<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" />
你可以把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)
}
}
将 .ttf 文件放入 assets 文件夹并在 onCreate 方法中使用此代码
fontTypeFace= Typeface.createFromAsset(getAssets(),
"fontawesome-webfont.ttf");
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setTypeface(fontTypeFace,0);
我正在使用带有 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,我找到了可能解决我的问题的方案。
Get the child views of the PagerTabStrip and check if it is an instance of a TextView. If it is, set the typeface:
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属性...
像这样:
<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" />
你可以把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)
}
}
将 .ttf 文件放入 assets 文件夹并在 onCreate 方法中使用此代码
fontTypeFace= Typeface.createFromAsset(getAssets(),
"fontawesome-webfont.ttf");
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setTypeface(fontTypeFace,0);