我正在尝试在我的 ActionBar
标签上设置自定义字体 .
我已经看到更多的开发人员要求在SO上采用正确的方法(例如How to customize the font of Action Bar tabs&How (if possible) could I set a custom font in a ActionBar on tab text with a font in my assets folder?),但没有答案 .
到目前为止,我遵循了两种方法:
1) The first one was inspired by this SO question and consists of inflating a custom layout for each tab:
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,则选项卡 Headers 将显示在溢出列表(Spinner / Drop-down)中,但所选值显示为空 . 当您单击此列表的项目时,所有这些视图都会消失 . 当例如用户扩展搜索动作视图时导致android将标签显示为下拉列表时,这尤其令人讨厌 .
2) I've tried another approach as presented here which involves using a 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));
类TypefaceSpan可以看here .
So...
有没有人知道使用"/assets/fonts/..."字体设置 ActionBar
标签 Headers 的正确方法?任何帮助将不胜感激 .
EDIT:
更多关于第二种方法 . 我正在使用的TypefaceSpan类实际上是android.text.style.TypefaceSpan的一个分支,由用户@twaddington在这里提供:How to Set a Custom Font in the ActionBar Title?
EDIT 2:
在上一个链接中,评论说明:"if the textAllCaps attribute is set to true on the underlying TextView (e.g. via a theme), then the custom font won't appear. This was an issue for me when I applied this technique to the action bar tab items" .
我已经改变了我的样式,因此textAllCaps设置为false,现在第二种方法似乎有效 . 我会测试一下并发布结果 .
Conclusion:
上一个编辑中的解决方案似乎有效 .
将@ CommonsWare的答案标记为其相关性是正确的 .
PS-EDIT for @PeteH:
我6个月前问过这个,所以我不记得所有的细节 . 我相信对于这个应用程序,我最终采用了不同的导航方法 . 我现在可以在应用程序中找到的所有内容(关于刷卡......)是 Activity
,其布局包含 ViewPager
,带有 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中,我有Actionbar的Tab Text的样式,如下所示:
<!-- 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>
2 回答
TypefaceSpan
仅适用于三种内置字体 .话虽如此,forking TypefaceSpan使用从本地文件路径加载
Typeface
的那个应该不那么难 . 不是将构造函数中的String
视为面部族名称,而是将其视为本地文件路径,调整apply()
以从那里加载它 .Typeface
本身不是Parcelable
,因此您需要使用该路径 .从资产中获取
Typeface
的问题是TypefaceSpan
需要访问AssetManager
,并且在从Parcel
放入并重建后,它不会轻易访问一个 .我没有使用过你的第一种技术,但我对你遇到问题并不感到惊讶 .
您可能还会考虑完全删除操作栏标签并切换到带有选项卡式指示器的
ViewPager
,因为您可以更轻松地设置样式,例如,Jake Wharton的TabPageIndicator
.首先看一下这个例子:[http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/][1]
然后创建一个布局并使用以下代码将其命名为tab_title:
然后只需在Androidhive项目的MainActivity的onCreate()方法中,只需更改:
至:
(无需说你必须将fonts / titr.TTF更改为你的资产目录和文件名)联合它!!