Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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@L_262_29@提供的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的样式,如下所示:

本图文内容来源于网友网络收集整理提供,作为学习参使用,版权属于原作者。

大佬总结

以上是大佬教程为你收集整理的java – ActionBar选项卡标题中的自定义字体全部内容,希望文章能够帮你解决java – ActionBar选项卡标题中的自定义字体所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。