Android font change method-typeface

Source: Internet
Author: User

In addition to changing the font size, android can also change the font style. The method is very simple. You only need to copy the corresponding font to the Assets Directory edge.

The procedure is as follows:

1. after preparing the font, copy a font in Windows/fonts and paste it to the Assets Directory of the eclipse project. The Assets Directory can store any resource files, create a new fonts directory and store the copied font as needed.

2. Edit main. XML, add a textview and two buttons, and set the ID respectively.

<? XML version =" 1.0 "Encoding =" UTF-8 "?> <Linearlayout xmlns: Android =" Http://schemas.android.com/apk/res/android "Android: Orientation =" Vertical "Android: layout_width =" Fill_parent "Android: layout_height =" Fill_parent "> <Textview Android: layout_width ="Fill_parent "Android: layout_height =" Wrap_content "Android: text =" @ String/tvtext "Android: Id =" @ + ID/tvtext "/> <Button Android: layout_width =" Wrap_content "Android: Id =" @ + ID/btnchangefontstyle "Android: layout_height =" Wrap_content "Android: text =" @ String/btnchangestyle_text "> </Button> <button Android: layout_width =" Wrap_content "Android: Id ="@ + ID/btnchangefontsize "Android: layout_height =" Wrap_content "Android: text =" @ String/btnfontsize_text "> </Button> </linearlayout>

3. Edit and change the font and size accordingly.Code

Package gphone. ex03_13; import android. app. activity; import android. app. alertdialog; import android. content. dialoginterface; import android. graphics. typeface; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. textview; public class ex03_13 extends activity {textview tvtext = NULL; button btnchangefontstyle = NULL; button btnchangefontsize = NULL;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); tvtext = (textview) findviewbyid (R. id. tvtext); btnchangefontstyle = (button) findviewbyid (R. id. btnchangefontstyle); btnchangefontsize = (button) findviewbyid (R. id. btnchangefontsize); btnchangefontstyle. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubTvtext. settypeface (typeface. createfromasset (getassets (), "fonts/stcaiyun. TTF ")); }); Btnchangefontsize. setonclicklistener (New View. onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stub Tvtext. settextsize (50 ); }});}}
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.