In fact, the use of custom fonts on Android is very simple, under the Assets folder, you define a font folder, and then, put their own fonts in, you can rename it,
Then, in the code, you can set it up as follows:
Typeface Typeface = Typeface.createfromasset (_instance.getassets (), "Fonts/mi4.ttf"); Textview.settypeface (Typeface );
In this way, the text displayed by TextView is our custom font.
But in fact, there are some problems, I say one by one:
Font offset: Just start, Our boss gave us a XX.OTF format font, Apple use no problem, Android, you can change the suffix name, change to TTF, can also be used, but the problem is that the font is lower than the whole, using the original font, can be displayed normally, but using this font, only display on Ban LA, more critical is, English and Chinese The offset is not the same, I tuned for a long, finally found that the font is the problem, you must use the original TTF font, using the software to convert the OTF font to TTF also does not, will still biased.
Workaround: Use native TTF fonts
Stutter, code duplication: As you know, using the above code, you can set the font for TextView, but so, can't set the global font, every control, to set this, my app has a lot of interfaces, there are many controls, I have each set, the result, the interface is very card. Will overflow as well.
And, in this way, there will be a lot of code duplication.
These two questions, my Final solution is as follows:
First: Custom Applicaion, now, is generally the custom application, and then, in the program as follows:
public class MyApplication extends application { private Typeface Typeface; private static MyApplication _instance; @Override public void OnCreate () { super.oncreate (); _instance = (MyApplication) getapplicationcontext (); typeface = Typeface.createfromasset (_instance.getassets (), "Fonts/mi4.ttf"); } public static MyApplication Getinstace () { return _instance; } Public Typeface Gettypeface () { return Typeface; } public void Settypeface (Typeface Typeface) { this.typeface = Typeface; }}
As the following code shows, I directly declare a typeface, it has its own Getter,setter method, at the same time, at the beginning of the program, I use the following code, the beginning of the typeface.
Then, use the custom view:
public class Mytextview extends TextView {public Mytextview (context context) { super (context); Settypeface (Myapplication.getinstace (). Gettypeface ()); } Public Mytextview (context context, AttributeSet Attrs) { Super (context, attrs); Settypeface (Myapplication.getinstace (). Gettypeface ()); } Public Mytextview (context context, AttributeSet attrs, int defstyleattr) { Super (context, attrs, defstyleattr); Settypeface (Myapplication.getinstace (). Gettypeface ());} }
, in the custom view construction method, the typeface in MyApplication is used directly to set the font for the view.
So, in all the interfaces, we can use our custom TextView, we test and the software is now online, no problem at all.
The other, if Edittext,button, and so on, the same.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android custom control font, resolve font offset, stutter, code duplication and other issues