How to set the font of all controls in the current view

Source: Internet
Author: User

In this example, set the font style for all the controls on the interface at one time. The idea is to find the parent control and traverse the child control. If the child control is a control that can modify text, set text. This applies to control inheritance. Many controls are inherited from textview, so convert the controls into textview and set the font.

Layout File

<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: Id = "@ + ID/layout" Android: layout_width = "match_parent" Android: layout_height = "match_parent" tools: context = "$ {relativepackage }. $ {activityclass} "> <textview Android: Id =" @ + ID/textview1 "Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "Android: layout_a Lignparenttop = "true" Android: layout_centerhorizontal = "true" Android: layout_margintop = "113dp" Android: text = "small text" Android: textappearance = "? Android: ATTR/textappearancesmall "/> <edittext Android: Id =" @ + ID/edittext1 "Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "Android: layout_below = "@ + ID/textview1" Android: layout_centerhorizontal = "true" Android: layout_margintop = "41dp" Android: hint = "Kale" Android: gravity = "center_horizontal" Android: EMS = "10"> <requestfocus/> </edittext> <button Android: Id = "@ + ID/button1" androi D: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignleft = "@ + ID/textview1" Android: layout_below = "@ + ID/edittext1" Android: layout_margintop = "50dp" Android: text = "button"/> <textview Android: Id = "@ + ID/textview2" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignparenttop = "true" Android: layout_alignright = "@ + ID/textview1" Andro ID: text = "set text size" Android: textappearance = "? Android: ATTR/textappearancesmall "/> <seekbar Android: Id =" @ + ID/seekbar "Android: layout_width =" match_parent "Android: layout_height =" wrap_content "Android: layout_below = "@ + ID/textview2" Android: layout_centerhorizontal = "true" Android: layout_margintop = "27dp" Android: max = "40" Android: progress = "16"/> </relativelayout>

 

Mainactivity

Package COM. kale. font; import android. app. activity; import android. graphics. typeface; import android. OS. bundle; import android. view. view; import android. view. viewgroup; import android. widget. button; import android. widget. edittext; import android. widget. seekbar; import android. widget. seekbar. onseekbarchangelistener; import android. widget. textview; public class mainactivity extends activity {Private Static int Flag = 0; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); // sets fonts for all final typeface mfont = typeface. createfromasset (getassets (), "Roboto-Thin.ttf"); Final viewgroup root = (viewgroup) findviewbyid (R. id. layout); setfont (root, mfont, 16); flag = 1; // After initialization, the flag is set to 1, and the slide slider only modifies the font size. The specified font is not repeated. (Seekbar) findviewbyid (R. id. seekbar )). new feature () {@ override public void onstoptrackingtouch (seekbar) {}@ override public void onstarttrackingtouch (seekbar) {}@ override public void onprogresschanged (seekbar, int progress, Boolean fromuser) {setfont (root, mfont, Progress) ;}});}/** sets the font on all textviews in the viewgroup. searches recursively for * All inner viewgroups as well. just add a check for any other views you * want to set as well (edittext, etc .) */Public void setfont (viewgroup group, typeface font, int textsize) {int COUNT = group. getchildcount (); view V; For (INT I = 0; I <count; I ++) {v = group. getchildat (I); If (V instanceof textview | V instanceof edittext | V instanceof button) {If (flag = 0) {(textview) V ). settypeface (font);} (textview) V ). settextsize (textsize);} else if (V instanceof viewgroup) setfont (viewgroup) V, Font, textsize );}}}

 

Source code download: http://download.csdn.net/detail/shark0017/7732339

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.