简体   繁体   English

如何为Android的操作栏或活动标题设置自定义字体?

[英]How to Set Custom Font for Action bar or Activity title Android?

I am trying to change font for Activity title, but its not working. 我正在尝试更改活动标题的字体,但无法正常工作。 Please suggest? 请提出建议? I am trying like this 我正在尝试这样

SpannableString s = new SpannableString("LOGIN");
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Regular.ttf");
s.setSpan(tf, 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// Update the action bar title with the TypefaceSpan instance
ActionBar actionBar = getActionBar();
actionBar.setTitle(s);

I know this has been asked a very long time ago, but this might help others. 我知道这个问题已经很久以前问过了,但这可能会对其他人有所帮助。 Apparently, you can't use the typeface object as an input to the setSpan method, although it does take an Object as its first argument (and everything is an Object), but to set a custom font the first argument needs to be TypefaceSpan object: 显然,您不能将字样对象用作setSpan方法的输入,尽管它确实将Object作为其第一个参数(并且一切都是对象),但是要设置自定义字体,则第一个参数需要是TypefaceSpan对象:

s.setSpan(new TypefaceSpan(***), 0, s.length, flag); // whatever flag you are using

The problem is, TypefaceSpan takes either a String or a Parcelable, not a typeface object, so as far as I understand, you can't set a custom font that is created from an asset for example. 问题是,TypefaceSpan接受String或Parcelable而不是Typeface对象,据我所知,例如,您不能设置从资产创建的自定义字体。 This is where I am stuck actually because I am looking for a solution for this rather than the solution suggested here by "notme" which is extending TypefaceSpan object, although it probably works but I am looking for a more simpler and elegant way to do this. 这实际上是我被困的地方,因为我正在寻找一个解决方案,而不是扩展TypefaceSpan对象的“ notme” 在这里建议的解决方案,尽管它可能起作用,但是我正在寻找一种更简单,更优雅的方法。

Good luck and happy coding. 祝你好运,编码愉快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM