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
I would suggest extending TextView, and always using your custom TextView within your XML layouts or wherever you need a TextView. In your custom TextView, override
setTypeface
Working for Xamarin.Android:
Class:
Application Implementation:
Style:
I would like to improve weston's answer for API 21 Android 5.0.
Cause
Under API 21, most of the text styles include fontFamily setting, like:
Which applys the default Roboto Regular font:
The original answer fails to apply monospace font, because android:fontFamily has greater priority to android:typeface attribute (reference). Using Theme.Holo.* is a valid workaround, because there is no android:fontFamily settings inside.
Solution
Since Android 5.0 put system typeface in static variable Typeface.sSystemFontMap (reference), we can use the same reflection technique to replace it:
Calligraphy works pretty well, but it is not suitable for me, since it does not support different weights (bold, italic, etc) for a font-family.
So I tried Fontain, which allows you to define custom Views and apply them custom font families.
in order to use Fontain, you should add the following to your app module build.gradle:
Then, instead of using regular TextView, you should use FontTextView
Example of FontTextView with uppercase and bold content:
You can set custom fonts for every layout one by one ,with just one function call from every layout by passing its root View.First ,create a singelton approach for accessing font object like this
You can define different fonts in above class, Now Define a font Helper class that will apply fonts :
In the above code, I am applying fonts on textView and EditText only , you can apply fonts on other view elements as well similarly.You just need to pass the id of your root View group to the above apply font method. for example your layout is :
In the Above Layout the root parent id is "Main Parent " now lets apply font
Cheers :)
This solution does not work correctly in some situations.
So I extend it:
FontsReplacer.java
https://gist.github.com/orwir/6df839e3527647adc2d56bfadfaad805