Android Development different font settings

Source: Internet
Author: User

  Control of different text fonts in development is mainly the use of typeface objects

Because it is a simple demo, are using the Android native components, although ugly, you can learn something is OK! (if there are errors or deficiencies, please enlighten me, thank you!) )

Let's take a look at the simple running effect.

      

         

That is, setting two buttons, one TextView, and clicking a different button triggers a different event. Directly on the code

Mainactivity.java

1  Public classMainactivityextendsActivityImplementsOnclicklistener {2 3     PrivateButton changesize;4     PrivateButton Changefont;5     PrivateTextView TV;6 7 @Override8     protected voidonCreate (Bundle savedinstancestate) {9         Super. OnCreate (savedinstancestate);Ten Initview (); One     } A  -     Private voidInitview () { - requestwindowfeature (window.feature_no_title); the Setcontentview (r.layout.activity_main); -TV =(TextView) Findviewbyid (r.id.tv); -Changesize =(Button) Findviewbyid (r.id.changesize); -Changefont =(Button) Findviewbyid (r.id.changefont); +Changesize.setonclicklistener ( This); -Changefont.setonclicklistener ( This); +     } A  at @Override -      Public voidOnClick (View v) { -         Switch(V.getid ()) { -          Caser.id.changesize: -             //use Settextsize to change font size in SP -Tv.settextsize (20); in              Break; -          CaseR.id.changefont: to             //must time under assets create a Fonts folder put in the font file (. ttf) you want to use, providing a relative path to createfromassets () to create the typeface object +Tv.settypeface (Typeface.createfromasset (Getassets (), "Fonts/song.ttf")); -         default: the              Break; *         } $     }Panax Notoginseng}

Activity_main.xml

1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5Tools:context= ". Mainactivity ">6 7<TextView8Android:id= "@+id/tv"9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" Oneandroid:text= "@string/hello_world" AAndroid:textsize= "13sp"/> -  -<Button theAndroid:id= "@+id/changesize" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -Android:layout_alignparentleft= "true" +android:layout_below= "@+id/tv" -android:layout_margintop= "15DP" +android:text= "@string/changesize"/> A  at<Button -Android:id= "@+id/changefont" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -Android:layout_alignbaseline= "@+id/changesize" -Android:layout_alignbottom= "@+id/changesize" inandroid:layout_marginleft= "16DP" -android:layout_torightof= "@+id/changesize" toandroid:text= "@string/changefont"/> +  -</RelativeLayout>

By placing the external font file under fonts/, you can use Assetsmanage to reference external resources

In addition to the use of Createfromasset to construct typeface, but also through the Defaultfromstyle to use the Android built-in several typeface, here no longer repeat, the following is the type of typeface hierarchy, You can see a few common methods.

        

Code Download: Https://github.com/SamSarah1/AndroidDemo

   

Android Development different font settings

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.