Android app uses custom fonts to implement the method _android

Source: Internet
Author: User
Tags stub

Android System built-in fonts

The Android system itself has built-in fonts that you can use in your programs and support the style of modifying fonts when XML configuration TextView. Support Fields Android:textstyle, Android:typeface, android:fontfamily, System built in Normal|bold|italic three style, built in Normal,sans,serif , monospace, several fonts (measured in English only valid), Typace and fontfamily function.

Use a custom font

This way you can change the style of the font, but not the real customization. The Android system supports typeface, or TTF font files. We can put the TTF font file in the program and use typeface to set the font in the program.

The first step is to create a new fonts directory under the assets directory and put TTF font files here.

In the second step, the program calls:

public class Mainactivity extends Appcompatactivity {
   private TextView TextView;
   @Override
   protected void onCreate (Bundle savedinstancestate) {
     super.oncreate (savedinstancestate);
     Setcontentview (r.layout.activity_main);
     textview= (TextView) Findviewbyid (r.id.text);
     Assetmanager assets = getassets ();
     Typeface Fromasset = Typeface.createfromasset (assets, "Fonts/fzlt.ttf");
     Textview.settypeface (Fromasset);
  }

Note The TTF file name cannot be used in Chinese, or it may not be loaded.

For more places to use, you can write a TextView subclass to unify the processing.

public class Customtextview extends TextView {public
 
   Customtextview (context) {
     super);
     TODO auto-generated constructor stub
   } public
 
   Customtextview (context context, AttributeSet attrs) {
     Super (CONTEXT,ATTRS);
     TODO auto-generated constructor stub
   } public
 
   Customtextview (context context, AttributeSet Attrs,int Defstyle) {
     super (Context,attrs,defstyle);
     TODO auto-generated constructor stub
   } public
 
   void Settypeface (typeface tf, int style) {
     Super.settypeface (Appcontext.getinstance (). Gettypeface ());
   }
 

Initialize Custom fonts
typeface = Typeface.createfromasset (Getassets (), "Fonts/fzlt.ttf");

The method is still a bit flawed: you can only replace the font for a class of controls, and if you need to replace the font for a button or EditText control, you need to customize the controls in the same way so that you can do a lot of work and efficiently replace the fonts in the entire app, see resources below.

Using custom fonts in WebView

For a local Web page, place the font file in the Asset directory and add the following in the CSS, customizing a font face and using the font face if needed.

<style>
@font-face {
   font-family: ' Myface ';
   Src:url (' File:///android_asset/fonts/fzlt.ttf ');
}
body {
   margin:0;
   padding:0;
   font-family: ' Myface ', ' founder Orchid Pavilion fiber Black simplified ';
textbar{box-sizing:border-box; width:100%; padding:5px;}
. Textbar p{font-size:16px; text-align:justify; color: #333; line-height:24px; margin:0 0 0 0;}
. Textbar h1{font-size:18px margin:10px 0 10px 0;color: #000}
</style>

For online web pages, you need to put the font file on the server, use the same way to define the font face, apply to each place.
To reduce Web pages or server-side work, you can inject font-face CSS in a local injection mode and replace the entire page with style. To WebView custom webviewclient, rewrite Onpagefinish and add the following:

View.loadurl ("Javascript:!function () {" + "s=document.createelement (' style '); s.innerhtml=" + "\" @font-face{ Font-family:myhyqh;src:url (' **injection**/hyqh.ttf ');} *{FONT-FAMILY:MYHYQH!important;} \ ";" + "document.getElementsByTagName (' head ') [0].appendchild (s);" + "document.getelementsbytagname (' body ') [0]. style.fontfamily = \ "Myhyqh\";}
 
()"); Since there is no access to the local asset folder on the Web page, we need to intercept the request to load the local file, where I have replaced ' file://android_assets/' as ' **injection**/', and we need to rewrite ' Shouldinterceptrequest '//When requesting the font file for us, load the local file: @Override public webresourceresponse shouldinterceptrequest (
   WebView view, String URL) {webresourceresponse response = super.shouldinterceptrequest (view, URL);
   LOG.I ("Load intercept request:" + URL); 
     if (URL!= null && url.contains ("**injection**/")) {//string Assertpath = url.replace ("**injection**/", "");
     String Assertpath = url.substring (Url.indexof ("**injection**/") + "**injection**/". Length (), url.length ()); try {response = new Webresourceresponse ("APPlication/x-font-ttf "," UTF8 ", Getassets (). Open (Assertpath));
     catch (IOException e) {e.printstacktrace ();
} return response;



 }

Thank you for reading, I hope to help you, thank you for your support for this site!

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.