I need to use certain font for my entire application. I have .ttf file for the same. Is it possible to set this as default font, at application start up and then use it elsewhere in the application? When set, how do I use it in my layout XMLs?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How can I create this custom Bottom Navigation on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
For changing default font family of the TextViews, override textViewStyle in your app theme.
For using custom font in fontFamily, use font resources which is in support library.
The feature was added in Android 26 but backported to older versions via supportlib.
https://developer.android.com/guide/topics/resources/font-resource.html https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html#using-support-lib
I wrote a class assigning typeface to the views in the current view hierarchy and based os the current typeface properties (bold, normal, you can add other styles if you want):
Now in all fragments in onViewCreated or onCreateView, in all activities in onCreate and in all view adapters in getView or newView just invoke:
Yes with reflection. This works (based on this answer):
(Note: this is a workaround due to lack of support for custom fonts, so if you want to change this situation please do star to up-vote the android issue here). Note: Do not leave "me too" comments on that issue, everyone who has stared it gets an email when you do that. So just "star" it please.
You then need to overload the few default fonts, for example in an application class:
Or course if you are using the same font file, you can improve on this to load it just once.
However I tend to just override one, say
"MONOSPACE"
, then set up a style to force that font typeface application wide:API 21 Android 5.0
I've investigated the reports in the comments that it doesn't work and it appears to be incompatible with the theme
android:Theme.Material.Light
.If that theme is not important to you, use an older theme, e.g.:
Yes, its possible to set the font to the entire application.
The easiest way to accomplish this is to package the desired font(s) with your application.
To do this, simply create an assets/ folder in the project root, and put your fonts (in TrueType, or TTF, form) in the assets.
You might, for example, create assets/fonts/ and put your TTF files in there.
I would like to improve weston's and Roger Huang's answers for over API 21 Android lollipop with theme "Theme.AppCompat".
Below Android 4.4
Over(equal) API 5.0
And the FontsOverride util file is same as what in weston's answer. I have tested in these phones:
Nexus 5(android 5.1 Primary Android System)
ZTE V5(android 5.1 CM12.1)
XIAOMI note(android 4.4 MIUI6)
HUAWEI C8850(android 2.3.5 UNKNOWN)
I would also suggest extending TextView and other controls, but it would be better I consider to set up font in constructs.