setting fonts in Android via typeface

Source: Internet
Author: User

The Android system default font supports four fonts, respectively:

    1. Noraml (normal font, default font used by the system)
    2. Sans (non-liner font)
    3. Serif (serif font)
    4. Monospace (equal width font)

Other font files (*.ttf) can also be used

The difference between the following three types of fonts can be seen:
http://kb.cnblogs.com/page/192018/

first, using the system's own fonts

1. modifying fonts in XML

    <!--use the default sans font--<TextViewAndroid:id="@+id/sans"android:text="Hello,world"Android:textsize="20SP"Android:typeface="Sans"/><!--use the default serifs font--<textview android:id= "@+id/serif" android:text= "Hello,World" Span class= "Hljs-attribute" >android:textsize= "20sp"  Android:typeface= "serif"/> <!--use the default monospace font-- <textview android:id= "@+id/monospace" android:text= "Hello,World" android:textsize= "20sp"  Android:typeface= "monospace"/>        

2. Modifying fonts in Java code

First step: Get TextView instances

  //获取textView实例  TextView textView = findViewById(R.id.textview);

Step two: Set the font

   //设置serif字体   textView.setTypeface(Typeface.SERIF);   //设置sans字体   textView.setTypeface(Typeface.SANS_SERIF); //设置monospace字体 textView.setTypeface(Typeface.MONOSPACE);
second, in Android can introduce other fonts

The first step: Create a new fonts directory under the assets directory and place the TTF font file here,

Step two: Call in the program

//实例化TextViewTextView textView = findViewById(R.id.textview);//得到AssetManagerAssetManager mgr=getAssets();//根据路径得到TypefaceTypeface tf=Typeface.createFromAsset(mgr, "fonts/pocknum.ttf");//设置字体textView.setTypeface(tf);

setting fonts in Android via typeface

Related Article

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.