java – ActionBar选项卡标题中的自定义字体

我正在尝试在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)第一个受到SO question的启发,包括为每个标签充气自定义布局:

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"),s.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.addTab(actionBar.newTab().setText(s).setTabListener(this));

Class TypefaceSpan可以在here看到.

所以…

有没有人知道使用“/ assets / fonts / …”字体设置ActionBar选项卡标题样式的正确方法?任何帮助将不胜感激.

编辑:

更多关于第二种方法.
我正在使用的TypefaceSpan类实际上是用户@twaddington在这里提供的android.text.style.TypefaceSpan一个分支:How to Set a Custom Font in the ActionBar Title?

编辑2:

在上一个链接中,评论声明:“如果textAllCaps属性在底层TextView上设置为true(例如通过主题),那么自定义字体将不会出现.当我将此技术应用于此时,这对我来说是个问题.操作栏标签项“.

我已经改变了我的样式,因此textAllCaps设置为false,现在第二种方法似乎有效.我会测试一下并发布结果.

结论:

一个编辑中的解决方案似乎有效.

将@ CommonsWare的答案标记为其相关性是正确的.

PS编辑@PeteH:

我6个月前问过这个,所以我不记得所有的细节.我相信对于这个应用程序,我最终采用了不同的导航方法.我现在可以在应用程序中找到的所有内容(关于刷卡…)是一个Activity,其布局包含一个带有PagerTabStrip的ViewPager,我的样式如下:

// 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,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    actionBar.addTab(actionBar.newTab().setText(s).setTabListener(this));
}

…而在我的styles.xml中,我有Actionbar的Tab Text的样式,如下所示:


                

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...