Android automatically adjusts font size based on screen resolution

Source: Internet
Author: User

1. Get phone screen width and height in oncreate

1 New displaymetrics (); 2 Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); // Get Window Properties 3 int screenwidth = dm.widthpixels; // width of window 4 int screenheight = dm.heightpixels; // Window Height

2. Get the layout of activity after OnCreate

1 viewgroup viewgroup= (viewgroup)this. Findviewbyid (Android. r.id.content); 2 changeview.changeviewsize (ViewGroup, ScreenWidth, screenheight);

3, ChangeView code as follows

1  Public classChangeView {2     //Traverse Set Font3      Public Static voidChangeviewsize (ViewGroup ViewGroup,intScreenWidth,4             intScreenHeight) {//incoming activity top layout, screen width, screen height5         intAdjustfontsize =adjustfontsize (ScreenWidth, screenheight);6          for(inti = 0; I < Viewgroup.getchildcount (); i++) {7View v =Viewgroup.getchildat (i);8             if(VinstanceofViewGroup) {9 changeviewsize ((viewgroup) v, ScreenWidth, screenheight);Ten}Else if(VinstanceofButton) {//button to enlarge this must be placed on the TextView, because the button also inherits the TextView One(Button) v). Settextsize (Adjustfontsize + 2); A}Else if(VinstanceofTextView) { - ( (TextView) v). Settextsize (adjustfontsize); -                 /* the * if (v.getid () = = r.id.title_msg) {//Top header ((TextView) v - *). Settextsize (adjustfontsize+4); }else{((TextView) v - *). Settextsize (Adjustfontsize); } -                  */ +             } -         } +     } A  at     //Get font size -      Public Static intAdjustfontsize (intScreenWidth,intscreenheight) { -ScreenWidth = ScreenWidth < screenheight?Screenwidth:screenheight; -         /** - * 1. Get the view width in the onsizechanged of the view, normally the default width is 320, so calculate a zoom rate = (float) - * w/320 W is the actual width 2. Then paint.settextsize ((int) (8*rate) when setting the font size; in * 8 is the font size to be set at a resolution width of 320 actual font size = default font size x rate -          */ to         intRate = (int) (5 * (float) screenwidth/320);//I myself test this multiples more suitable, of course you can test and then modify +         returnRate < 15? 15:rate;//the font is too small to look good. -     } the } * //method is transferred from http://hy0664.iteye.com/blog/1360051

4. If you are developing an application that wants to browse on a tablet, add the manifest node in the Androidmanifest.xml file (DTD recommendations on the application node):

1 < Supports-screens 2         android:anydensity = "true" 3         android:largescreens= "true"4        android:normalscreens= " True "5        android:smallscreens=" true "6        android: resizeable= "true"/>

Android automatically adjusts font size based on screen resolution

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.