setting fonts in Android via typeface

Source: Internet
Author: User

setting fonts in Android via typeface

the Android system supports three fonts by default:"sans", "serif", "monospace", in addition to other font files (*.ttf)

Method One: Use the android default font in XML

& lt;! --   sans font -->
        <TextView    android:id= "@+id/sans"
                    android:text= "Hello,world"
                    android:typeface= "Sans"
                    android:textsize= "20sp"/>

<!--   serifs font -->
        <textview   android:id= " @+id/serif "
                    android:text= "Hello,world"
                    android:typeface= "Serif"
                    android: Textsize= "20sp"/>

<!--   using the default monospace Span style= "Font-family:simsun;" > font -->
        <textview   android:id= " @+id/monospace "
                    android:text= "Hello,world"
                    android:typeface= "Monospace"
                    android: Textsize= "20sp"/>

method Two : in Android you can introduce other fonts, first to save the font file in the assets/fonts/directory

1. <!--there is no font set, it will be set in Java code --

<textview android:id= "@+id/custom"
android:text= "Hello,world"
Android:textsize= "20sp"/>

2. Introduction of other font-critical code in Java programs

// Get TextView Control Object
TextView TextView = (TextView) Findviewbyid (r.id.custom);

//Save the font file in the assets/fonts/directory and create the typeface object

Typeface Typeface =typeface.createfromasset (Getassets (), "Fonts/handmadetypewriter.ttf");  

//Use fonts

Textview.settypeface (typeFace);  

setting fonts in Android via typeface

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.