Android References external fonts

Source: Internet
Author: User

When doing Android development, some software will require some special fonts, we need to introduce the external TTF format font into the program, the specific steps are:

Create a new fonts directory in the Android app directory assets, copy the TTF font file we need to use (the OTF format directly changes the suffix to TTF), then the code:

//  Save the font file in the assets/fonts/directory,  in   program    Typeface Typeface = Typeface.createfromasset (Getassets (), "Fonts/droidsansthai.ttf"  ); //  textview.settypeface (typeFace); 

Ps:1. If you want to make the font bold again on this basis, use android:textstyle= "bold" in the XML file of the interface configuration is futile, the wood has the effect, at this time how to do?

We can still control in the code, plus a line of code:

Et_note.getpaint (). Setfakeboldtext (true); it can be achieved.

If you want the entire interface to use the same font, you can use batch processing to add a new Java class, as follows:

The code is used to load all TextView-based text components (TextView, buttons, RadioButton, ToggleButton, and so on) without regard to the layout hierarchy of the interface. ( Large memory waste)

In addition TTF font library files are relatively large, will increase the volume of the installation package OH

public class FontManager {            public static void changeFonts(ViewGroup root, Activity act) {               Typeface tf = Typeface.createFromAsset(act.getAssets(),                  "fonts/xxx.ttf" );               for ( int i =  0 ; i < root.getChildCount(); i++) {               View v = root.getChildAt(i);               if (v  instanceof TextView) {                  ((TextView) v).setTypeface(tf);               else if (v  instanceof Button) {                  ((Button) v).setTypeface(tf);               else if (v  instanceof EditText) {                  ((EditText) v).setTypeface(tf);               else if (v  instanceof ViewGroup) {                  changeFonts((ViewGroup) v, act);               }           }            }   }  
更多:https://segmentfault.com/q/1010000000494116

Android References external fonts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.