我看到更多的开发者在SO上询问如何正确地实现这个功能(例如如何自定义Action Bar标签的字体和如果可能,我如何在选项卡文本中设置来自我的资产文件夹中的自定义字体?),但没有答案。
到目前为止,我已经尝试了两种方法:
1)第一种方法是受到这个SO问题的启发,它包括为每个选项卡填充一个自定义布局:
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.tab_title, null); // a custom layout for the tab title, basically contains a textview...
TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title);
titleTV.setText(mSectionsPagerAdapter.getPageTitle(i));
titleTV.setGravity(Gravity.CENTER_VERTICAL);
titleTV.setTypeface(((MyApp) getApplicationContext()).getCustomTypeface());
// ...Here I could also add any other styling I wanted to...
actionBar.getTabAt(i).setCustomView(customView);
这似乎不是一个很好的方法,因为如果选项卡和操作在横向模式下无法适应ActionBar,则选项卡标题将显示在溢出列表(Spinner / Drop-down)中,但所选值将显示为空。当您单击此列表项时,所有这些视图都会消失。例如,当用户扩展搜索操作视图时,会导致Android将选项卡显示为下拉菜单,这尤其令人讨厌。
2)我已经尝试了另一种方法,如here所示,其中涉及使用SpannableString,但字体没有更改为我的自定义字体。
SpannableString s = new SpannableString(mSectionsPagerAdapter.getPageTitle(i));
s.setSpan(new TypefaceSpan(this, "FontName.ttf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.addTab(actionBar.newTab().setText(s).setTabListener(this));
Class TypefaceSpan 可以在这里查看。
所以...
有没有人知道如何使用来自“/assets/fonts/...”的 Typeface 来为 ActionBar
标签的标题设置样式?任何帮助将不胜感激。
编辑:
更多关于第二种方法的信息。我正在使用的 TypefaceSpan 类实际上是用户 @twaddington 在这里提供的 android.text.style.TypefaceSpan 的分支:如何在 ActionBar 标题中设置自定义字体?
编辑 2:
在之前的链接中,有一条评论提到:“如果在底层TextView上将textAllCaps属性设置为true(例如通过主题),则自定义字体将不会出现。当我将这种技术应用于操作栏选项卡项目时,这是一个问题”。我已经更改了我的样式,使textAllCaps设置为false,现在第二种方法似乎可行。我会进行一些测试并发布结果。 结论: 先前的解决方案似乎可行。
将@CommonsWare的答案标记为正确,因为它的相关性。 PS-针对@PeteH的编辑: 我6个月前问过这个问题,所以我不记得所有细节。我相信对于这个应用程序,我最终采取了不同的导航方法。现在我在应用程序中找到的所有内容(关于滑动的...)都是一个包含ViewPager和PagerTabStrip的Activity,我将其样式设置为:
// Style the Tab Strip:
Typeface tf = ((MyApplication) getApplication()).getTabStripTypeface(); // Used this to keep a single instance of the typeface (singleton pattern) and avoid mem. leaks
PagerTabStrip strip = (PagerTabStrip) findViewById(R.id.pager_title_strip);
strip.setTabIndicatorColor(getResources().getColor(R.color.myColor));
strip.setDrawFullUnderline(true);
for (int i = 0; i < strip.getChildCount(); ++i) {
View nextChild = strip.getChildAt(i);
if (nextChild instanceof TextView) {
TextView textViewToConvert = (TextView) nextChild;
textViewToConvert.setAllCaps(false);
textViewToConvert.setTypeface(tf);
}
}
虽然问题不同,但与此提问中所述的问题有关。
我能找到的唯一相关代码是这个,在这里我设置了一个SpannableString
:
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
SpannableString s = new SpannableString(mSectionsPagerAdapter.getPageTitle(i));
s.setSpan(new TypefaceSpan(this, "FontName.ttf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.addTab(actionBar.newTab().setText(s).setTabListener(this));
}
...而我的styles.xml中,我将Actionbar的选项卡文本样式设置为这样:
<!-- action bar tabtext style -->
<style name="ActionBarTabText.MyApplication" parent="@android:style/Widget.Holo.ActionBar.TabText">
<item name="android:textAppearance">@android:style/TextAppearance.Holo.Medium</item>
<item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
<item name="android:textSize">15sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">false</item>
<item name="android:ellipsize">marquee</item>
<item name="android:maxLines">1</item>
</style>