Android modify their own program font method

  • 2020-05-10 18:51:50
  • OfStack

Android offers three fonts: "Sans", "serif" and "monospace".
1. Set the font in the Android XML file
You can use android:typeface, for example android:typeface= "monospace". In this example we are dealing with android in Activity :text= "Hello, World! "Hello" has been displayed in 4 ways respectively, which are "Sans", "serif", "monospace" and the system default mode (sans is adopted by default after the experiment). There are differences in English fonts, but it looks like there are no differences in Chinese fonts. The XML file is as follows:
< ? utf xml version encoding = = "1.0" "8"? >   < TableLayout... ... android: stretchColumns = "1" > < TableRow > < TextView   android:text= "sans:"   android:layout_marginRight= "4px"   android:textSize= "20sp"   / > < TextView android: text = "Hello World! Hello "android:typeface   =" sans "  < ! The font is used to specify the font >   android: 20 sp  /textSize =" > < /TableRow > ... ... Similarly, set two TableRow successively, and modify sans to serif, monospace... ...   < TableRow >   < TextView android: custom text = ":"... . / >   < TextView android:id= "@+id/c12_custom"   android:text= "Hello, World! Hello "  android:textSize=" 20sp "/ >   < /TableRow >   < /TableLayout >
2. Use other fonts
1) put the copy file copy of the new font under the assets/fonts/ directory, for example, we put "*.ttf" copy in the past.
2) we need to set widget to this font. Unfortunately, it cannot be done directly in XML file, so we need to write the source code.
TextView tv = (TextView)findViewById(R.id.c12_custom);
// get assert of app, getAserts(), by giving the relative path below assert/. In practice, the font library might exist on an SD card, and createFromFile() could be used instead of createFromAsset.
Typeface face = Typeface.createFromAsset (getAssets(), "fonts/ timesi.ttf");
tv.setTypeface (face);
In the simulator, I successively imported the font of Chinese characters, about 4M, but the system could not recognize the font, so it was not displayed. Then I tried to use the English font timesi.ttf, which was normal. Therefore, Android is not compatible with all TTF fonts. In particular, there will be some problems in the support of special Chinese fonts. For incompatible fonts, Android does not make any error, but cannot display properly. Generally speaking, we will use the default font provided by the system.
For the Chinese characters, we used the Chinese name at the beginning of 1, and there was an error. Later, we changed it to all lowercase English names so that there would be no error. Therefore, we need to pay attention to the file naming.

3, 1 some attention
Use other word library, will consume the space of the program, this is to be very careful. And sometimes they don't give you all the text you need.
For example, the omissions. When there is too much text, you can omit the following by using the ellipsis, which is "..." As a font, you can set it through the android:ellipsize property. If we need to use the ellipsis feature, we need to make sure that the font has an ellipsis. In addition, in order to keep the length 1 straight, Android will be filled. In addition to replacing the 1 character with an ellipse match, the following character will be replaced with a special Unicode character, 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF). This character does not occupy any visual position, but ensures that string has the same length. Not all fonts support this particular character and can cause some garbled characters.
Android supports international languages, but we still need to be careful with custom fonts.


Related articles: