为完整的 Android 应用程序添加自定义字体
在我的应用程序中,我需要对所有文本视图和编辑文本字段使用 helvetica 字体。除了对每个 textview 使用 settypeface 方法之外,还有什么方法可以做到这一点?任何建议都会有很大的帮助。
提前致谢 !
In my application I need to use helvetica font for all the textviews and edit-text fields. Is there any way to do this other than using settypeface method for every textview ? Any suggestion would be a great help.
Thanks in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我自己想出来了。这是我使用的代码。我创建自定义
TextView
,它将自定义字体作为默认字体。I figured it out by my self. This is the code I used. I create custom
TextView
which has custom font as default font.在您的活动中,在您致电之后
您应该运行一个方法来遍历小部件的整个层次结构并处理字体替换,例如;
提到的“overrideFonts”方法应该类似于;
在这个方案中, FONT_REGULAR 应该在某个地方安全地初始化,您可能会喜欢单例或其他方式来确保它正确初始化...
如果您使用像 MyAppActivity 这样的 Activity 子类(扩展 Activity),那么您不需要更改每个 Activity 类以进行此类自定义。相反,您可以切入它并覆盖该行为;
这样你就可以利用你的任何活动来产生共同的行为;
我希望它有帮助...
干杯!
In your activities, right after you call
you should run a method to go over the whole hierarchy of widgets and deal with the font substitution, like;
And the mentioned "overrideFonts" method should be something like;
In this scheme, FONT_REGULAR should be initialized somewhere safely, you may fancy a singleton or some other way to be sure that it is initialized properly...
If you use a subclass of Activity like MyAppActivity (extends Activity), then you dont need to change each and every Activity class for such customizations. Instead you may cut into it and override the behavior as such;
This way you can use any activity of yours to have common behavior;
I hope it helps...
Cheers!
创建一个样式并使用它的所有文本属性。
使用它:
上面是在所有活动中使用自定义字体进行自定义,您可以使用......
试试这个。
create a style and use it all text attributes.
Use it:
Above is to use custom fonts in all activity for customization you can use....
TRy this.
要在所有应用程序中应用自定义字体,只需创建以下活动:
其中 FontManager 这是管理 /xml/fonts.xml 中定义的所有字体的类,SimpleFactory 只是创建视图并将自定义字体应用到文本视图实例的每个视图的工厂。
/xml/fonts.xml
FontFactory - abastract 类,只需扩展它来创建您自己的工厂
FontManager - 只需将 /xml/fonts.xml 中定义的字体文件映射到 /assets 目录中放置的字体文件,并按字体系列名称和字体返回 TypeFace风格。
如需更多代码和示例,请查看此处
To apply custom font across all application, just create following activity:
where FontManager this is class that manage all Fonts that defined in /xml/fonts.xml and SimpleFactory just factory that create views and apply custom font to each view that instance of text view.
/xml/fonts.xml
FontFactory - abastract class, just extends it to create your own factory
FontManager - just map font files defined in /xml/fonts.xml to font files placed in /assets directory and return TypeFace by font family name and font style.
for more code and samples just look here
好的,你可以做到这一点,但你本质上想要制作的是一种地图格式的字体图集(它必须以 unicode 顺序运行,以 !"#$% '()x+,-./ 开头)。功能是什么要做的就是输入一个字符串并找出每个对应字母在图集中的位置,
这不太容易,但每个字符必须具有相同的长度和宽度,您可以有不同的长度,但这会困难得多。
Ok you can do it but what you essentially want to make is a font atlas in a kinda map format(it will have to run in unicode order which starts !"#$% '()x+,-./). What the function will do is take in a string and work out where in the atlas each corresponding letter is.
Its not too easy but each character must have the same length and width, you could have different lengths but that will be a lot harder.
您可以使用 PixlUI http://bit.ly/1bzjWQn
将其 .jar 导入到您的项目中。在 XML 中使用它
不要忘记实现方案( xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui" )
You can use PixlUI at http://bit.ly/1bzjWQn
import their .jar in your project. Use it in XML
Don't forget implement scheme ( xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui" )