Implementation method of android app using custom font

Built in font of Android system

The Android system itself has built-in fonts that can be used in programs, and supports modifying font styles when configuring textview with XML. The supported fields are Android: TextStyle, Android: typeface and Android: fontfamily. The system has built-in three styles: normal, bold and italic. Normal, sans, serif and monospace are built-in. Several fonts (actually, these fonts are only valid in English). The functions of typespace and fontfamily are the same.

Use custom fonts

The above methods can change the font style, which is not really customized. The Android system supports typeface, that is, TTF font files. We can put TTF font file in the program and use typeface to set the font in the program.

First, create a new fonts directory under the assets directory and put the TTF font file here.

The second step is to call in the program:

Note that the TTF file cannot be named in Chinese, otherwise it may not be loaded.

For places that need to be used more, you can write a subclass of textview to handle them uniformly.

//Initialize custom font typeface = typeface createFromAsset(getAssets(),"fonts/fzlt.ttf");

The method still has some disadvantages: you can only replace the font of one type of control. If you need to replace the font of button or EditText controls, you need to customize these controls in the same way. This is a heavy workload. See the resources below for how to effectively replace the font in the whole app.

Use custom fonts in WebView

For local web pages, put font files in the asset directory, add the following contents in CSS, customize a font face, and use the font face where necessary.

For online web pages, you need to put the font file on the server, define the font face in the same way, and apply it everywhere. In order to reduce the work of web pages or server-side, you can inject font face CSS by local injection and replace the style of the whole web page. Customize webviewclient for WebView, rewrite onpagefinish, and add the following contents:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>