1. set the system default font in the Android XML file
You can use Android:typeface to set fonts in an XML file , such as android:typeface= "monospace". In this example, we in the activity of android:text= "Hello, world! hello " respectively carried out four kinds of display methods, followed by "Sans","serif"," Monospace "and system default mode (using Sans by default ). English fonts are different, seemingly Chinese fonts do not differ. The XML file is as follows:
<?xml version= "1.0″encoding=" utf-8″?> <tablelayout android:stretchcolumns = "1" > <TableRow> <Tex TView android:text="sans:" android:layout_marginright="4px" android:textsize= "20sp"/> < TextView android:text= "Hello, world! = "sans" <!–android:typeface used to specify font –> android:textsize= "20sp"/> </TableRow> < tablerow> <textview android:text= "Custom:" /> <textview android:id= "@+id/c12_custom" Android:te XT= "Hello, world! Hello "android:textsize=" 20sp "/> </TableRow> </TableLayout>
2. Use other fonts
1) Copy the TTF file of the new font to the assets/fonts/directory, for example, we copy the "*.ttf" to the past.
2) We need to set the widget to that font, which, unfortunately, cannot be done directly in the XML file and requires writing the source code.
TextView TV = (TextView) Findviewbyid (R.id.c12_custom); // Typeface face = Typeface.createfromasset (Getassets (), "fonts/Timesi.ttf"); Tv.settypeface (face);
imported Chinese Xingkai fonts in the simulator, about 4M , but the system does not recognize the font, does not appear, and then tries to use the English font Timesi.ttf, Normal. Therefore, Android is not compatible with all TTF fonts, especially in the Chinese special font support will be problematic, for incompatible fonts,Android can not error, but not normal display. In general, we will use the system's default provided body.
For the Chinese Xingkai font, we began to use the file is a Chinese name, error, and then we will change it to full lowercase English name will not be wrong, so in the file naming needs attention.
The
3, some note
Using other fonts, will consume the program space, which is very important to note. And these fonts sometimes don't give you exactly the text you need.
For example, omit. When the text is too much, you can omit the following by ellipsis, the ellipsis is the use of 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 guarantee the length of the android will be filled processing, in addition to a character to omit the match, the following character will be replaced by a special unicode character, Span lang= "en-us" > ' ZERO WIDTH no-break SPACE ' (U+feff). This character takes up any visual position, but guarantees that the string has the same length. Not all fonts support this special character and may cause some garbled behavior. The
android is supported by international languages, but we still need to be careful with custom fonts.
4, if a program needs to modify the font, you can use the custom TextView, code as shown below (2), define the method definition font , in order to avoid n more places to use Settypeface to set the font, Very troublesome.
The code used is shown in table (1)
Table (1):<com.cn21.esafe.utils.CustomFontTextView android:id= "@+id/title" Android:layout_width= "Wrap_content" android:layout_height= "Match_parent" Android:layout_centerinparent= "true" android:gravity= "center" android: TextColor= "@color/white" android:textsize= "@dimen/text_title"/>
Table (2): Packagecom.cn21.esafe.utils;ImportAndroid.content.Context;ImportAndroid.graphics.Typeface;ImportAndroid.util.AttributeSet;ImportAndroid.widget.TextView; Public classCustomfonttextviewextendsTextView {Private StaticTypeface Typeface; PublicCustomfonttextview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); Setcustomtypeface (context); } PublicCustomfonttextview (Context context, AttributeSet attrs) {Super(context, attrs); Setcustomtypeface (context); } PublicCustomfonttextview (Context context) {Super(context); Setcustomtypeface (context); } Private voidSetcustomtypeface (Context context) {if(typeface = =NULL) {typeface=Typeface.createfromasset (Context.getassets (),"Fonts/fangzhenglanting.ttf"); } settypeface (typeface); } Public StaticTypeface Gettypeface (context context) {if(typeface = =NULL) {typeface=Typeface.createfromasset (Context.getassets (),"Fonts/fangzhenglanting.ttf"); } returntypeface; }}
(37) Modifying program fonts in Android development