In search of the relevant data on the Internet, the two methods are considered to be a relatively fast way to change the program font, first of all, the introduction of two methods.
First, the first method is to override the control (take TextView as an example):
1, Android in the process of writing Google has already been the default of all the fonts are the specific font, they did not go to study, so if you want to let the program's font into the font you want is also not easy things, first you have to download the font library (suffix ttf), the English font library is not too big, In general, the Chinese font library is very large, so you can more programs inside the text, the font library to crop. To download the font library can be downloaded online, a bunch of on-line, csdn inside the comparison of stingy, font library incredibly still want points, do not want to spit groove.
2, after downloading the font library, your project directory assets folder, create a new folder "fonts" for the font library, and then add the Font library (. ttf) to the Fonts folder, of course, your font library file can not be named in Chinese.
3, rewrite TextView, create a class to inherit TextView, the code is as follows:
public class Chinesetextview extends TextView {public Chinesetextview (context context, AttributeSet Attrs) {super ( context, attrs);/* You must create a Fonts folder under assets and put in the font file (. ttf) you want to use * and provide a relative path to Creatfromasset () to create Typeface objects */typeface Fontface = Typeface.createfromasset (Context.getassets (), "Fonts/microsoft.ttf");//font file must be True Type font format (TTF);// When using an external font but found that the font has not changed (in the case of the Droid Sans), usually because//this font Android is not supported, and not your program error settypeface (Fontface);}}
4, in the XML layout file directly with this class instead of TextView can:
<com.example.androidsetting.chinesetextview android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:text= "TextView"/>
In the second way, this method does not need to inherit textview, but it still requires a font library.
The first two steps are the same as above, here's a copy.
1, Android in the process of writing Google has already been the default of all the fonts are the specific font, they did not go to study, so if you want to let the program's font into the font you want is also not easy things, first you have to download the font library (suffix ttf), the English font library is not too big, In general, the Chinese font library is very large, so you can more programs inside the text, the font library to crop. To download the font library can be downloaded online, a bunch of on-line, csdn inside the comparison of stingy, font library incredibly still want points, do not want to spit groove.
2, after downloading the font library, your project directory assets folder, create a new folder "fonts" for the font library, and then add the Font library (. ttf) to the Fonts folder, of course, your font library file can not be named in Chinese. 、
3, first create a method, Changefont (ViewGroup root) {}, used to traverse the current page of the control, the purpose is to give the same type of control style settings, so this method can also be relatively fast font settings
protected void Changefont (ViewGroup root) {Typeface tf = Typeface.createfromasset (This.getassets (), "Fonts/microsoft . TTF "); for (int i = 0; I <root.getchildcount (); i++) {View v = root.getchildat (i); if (v instanceof button) {(button) v). Settypeface (TF); (Button) v). Settextsize (15); (Button) v). SetTextColor (Color.gray); } else if (v instanceof TextView) {((TextView) v). Settypeface (TF); ((TextView) v). Settextsize (15); ((TextView) v). SetTextColor (Color.gray); } else if (v instanceof EditText) {((EditText) v). Settypeface (TF); ((EditText) v). Settextsize (15); ((EditText) v). SetTextColor (Color.gray); } else if (v instanceof viewgroup) {Changefont ((viewgroup) v); } } }
4, in the OnCreate method, of course, in Setcontentview (R.layout.activity_main);
ViewGroup root= (ViewGroup) This.getwindow (). Getdecorview (); Gets the outermost control Changefont (root) under this activity;
The above two methods are introduced, the following summarizes the two methods of feeling, I think each has advantages and disadvantages.
The first method, do not need to add code in the activity, directly modify the control of the line, but, because a page has the need to control all have text, such as Button,checkbox, a bunch, so if you change all the words are not to re-all these controls. The second way to add code to the activity is to add code to all of the activity, and then it's gone. The first method, in fact, is in the XML to achieve, that is, your control every time you have to TextView once, with ScrollView to explain the problem, scrollview inside there are many TextView, are the first way to modify the font, the first load of the interface , will load a period of time (see how much you load) to display the page, and then scrollview down drag, previously not seen TextView to load again, and then there will be a slight lag, this is the first method of the most fatal disadvantage. In contrast, the second method does not do this because it is the first loading interface in the modification. But again, these two methods, the first time you enter the interface will load a period of time to go in, the experience is not very good.
Android Modify App Font