我正在尝试在我的上设置自定义字体ActionBar
选项卡的标题。
我看到更多的开发人员要求以正确的方式在 SO 上执行此操作(例如如何自定义操作栏选项卡的字体 & 如何(如果可能)在选项卡文本的 ActionBar 中使用资产文件夹中的字体设置自定义字体?)但没有答案。
到目前为止,我采用了两种方法:
1)第一个是inspired这样所以问题包括为每个选项卡扩展自定义布局:
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,选项卡标题将显示在溢出列表(旋转器/下拉列表)中,但所选值显示为空的。当您单击此列表的项目时,所有这些视图都会消失。例如,当用户展开搜索操作视图导致 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));
可以看到类TypefaceSpanhere.
So...
有谁知道正确的造型方法ActionBar
选项卡标题的字体来自“/assets/fonts/...”?任何帮助将不胜感激。
EDIT:
更多关于第二种方法。
我正在使用的 TypefaceSpan 类实际上是android.text.style.TypefaceSpan正如用户 @twaddington 所介绍的:如何在 ActionBar 标题中设置自定义字体?
EDIT 2:
在之前的链接中,有评论指出:“如果底层 TextView 上的 textAllCaps 属性设置为 true(例如通过主题),则自定义字体将不会出现。当我将此技术应用于操作栏选项卡项时,这对我来说是一个问题”.
我更改了样式,将 textAllCaps 设置为 false,现在第二种方法似乎可以工作。我会稍微测试一下并发布结果。
结论:
先前编辑中的解决方案似乎有效。
将@CommonsWare 的答案标记为其相关性正确。
@PeteH 的 PS 编辑:
我六个月前问过这个问题,所以我不记得所有细节。我相信对于这个应用程序,我最终采用了不同的导航方法。我现在在应用程序中所能找到的(关于刷卡......)是Activity
其布局包含ViewPager
with a PagerTabStrip
,我的样式是这样的:
// 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 中,我的操作栏选项卡文本的样式如下:
<!-- 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>