Android adds font library and stroke setting method for textview

1、 Use the system's own font

Most developers of Android know that there is little support for fonts in Android. By default, the typeface attribute of textview supports sans, serif and monospace fonts. If no font is specified, the system will use sans as the font for text display. But the three fonts only support English, that is, as long as the text you display is Chinese, no matter which of the three fonts you choose, the display effect is the same.

1. Set in XML file

2. Set in Java code

Step 1: get the textview instance

Step 2: set font

2、 Add font library for textview

The Android system comes with font settings. These settings are settings for the display mode of the font, such as bold, tilt, underline, font size, etc., but they do not provide options for the font type, such as setting it to regular script, official script or elegant black. The Android system has only one fixed default font type, so if developers need to modify the font type, they must introduce their own font library.

1. Implementation of font library

Step 1: create a new fonts directory in the assets directory and put the TTF font file in this directory.

Step 2: implement in Java code

2. Effect drawing after importing font library

3、 Add stroke to textview

Textview, the default control of Android, is familiar to everyone, but the native textview does not support stroke effect. However, in the actual development process, we often encounter the need to add stroke for textview. Therefore, it is necessary to expand the native textview to support stroke textview with customized internal and external colors. The implementation principle of stroking effect is actually very simple. It is nothing more than obtaining the textpaint class, first painting the text content slightly larger than the default size, then painting the text content of the default size, and then setting two different colors through attributes, so as to produce stroking effect.

To add stroke to textview, several properties of textpaint are used:

Where stroketextview is an instance of custom textview. The code is as follows:

1. Add in constructor

2. Rewrite OnDraw method

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>