Android instance code that automatically adjusts font size based on resolution _android

Source: Internet
Author: User

Mobile devices are too many, the resolution is not the same, see most of the Internet to adapt to the font method is defined values320x480 or value-hdpi way to deal with.
Adopt the first kind of is miserable, the resolution of many equipments is different, do you want to define each kind?
Using the second type has no effect on the tablet computer.


Finally, the way the code is convenient and quick ...


[Java]

Copy Code code as follows:

Traverse Settings Font
public static void Changeviewsize (ViewGroup viewgroup,int screenwidth,int screenheight) {//Incoming activity top layout, screen wide, high screen
int adjustfontsize = adjustfontsize (screenwidth,screenheight);
for (int i = 0; I<viewgroup.getchildcount (); i++) {
View v = viewgroup.getchildat (i);
if (v instanceof viewgroup) {
Changeviewsize ((ViewGroup) v,screenwidth,screenheight);
}else if (v instanceof button) {//button increase this must be placed on the TextView, because the button also inherits the TextView
((Button) v). Settextsize (adjustfontsize+2);
}else if (v instanceof TextView) {
if (v.getid () = = r.id.title_msg) {//top title
((TextView) v). Settextsize (adjustfontsize+4);
}else{
((TextView) v). Settextsize (Adjustfontsize);
}
}
}
}


Get font Size
public static int adjustfontsize (int screenwidth, int screenheight) {
screenwidth=screenwidth>screenheight?screenwidth:screenheight;
/**
* 1. Get the width of the view in the onsizechanged of the view, typically the default width is 320, so calculate a scaling ratio
Rate = (float) w/320 W is the actual width
2. Then paint.settextsize ((int) (8*rate) when setting the font size; 8 is the font size to be set at a resolution width of 320
Actual font size = default font size x rate
*/
int rate = (int) (5* (float) screenwidth/320); I test this multiple more suitable, of course, you can test and then modify
Return rate<15?15:rate; The font is too small and not good-looking.
}

//Traversal settings font
public static void Changeviewsize (ViewGroup viewgroup,int screenwidth,int screenheight) {// Incoming activity top layer layout, screen wide, screen high
  int adjustfontsize = Adjustfontsize (screenwidth,screenheight);
  for (int i = 0; I<viewgroup.getchildcount (); i++) {
   View v = viewgroup.getchildat (i);
   if (v instanceof viewgroup) {
    changeviewsize (viewgroup) V,screenwidth, ScreenHeight);
  }else if (v instanceof button) {//button increase this must be placed on the TextView, because the button also inherits TextView
    ( (Button) v). Settextsize (adjustfontsize+2);
  }else if (v instanceof TextView) {
    if (v.getid () = r.id.title_msg) {//top title
     ((TextView) v). Settextsize (adjustfontsize+4);
   }else{
     ((TextView) v). Settextsize (adjustfontsize);
   }
  }
 }
 


Get font Size
public static int adjustfontsize (int screenwidth, int screenheight) {
screenwidth=screenwidth>screenheight?screenwidth:screenheight;
/**
* 1. Get the width of the view in the onsizechanged of the view, typically the default width is 320, so calculate a scaling ratio
Rate = (float) w/320 W is the actual width
2. Then paint.settextsize ((int) (8*rate) when setting the font size; 8 is the font size to be set at a resolution width of 320
Actual font size = default font size x rate
*/
int rate = (int) (5* (float) screenwidth/320); I test this multiple more suitable, of course, you can test and then modify
Return rate<15?15:rate; The font is too small and not good-looking.
}


Finally in Avtivity OnCreate after the call changeviewsize on the line ... The text is big then it corresponds to the background, so it is recommended that the background picture of the control with 9 house type of picture, look comfortable.
In addition, if you develop applications that want to be browsed on a tablet, please add the manifest node in the Androidmanifest.xml file (the DTD recommendation is placed above the application node):
[Java]
Copy Code code as follows:

<supports-screens
Android:anydensity= "true"
Android:largescreens= "true"
Android:normalscreens= "true"
Android:smallscreens= "true"
Android:resizeable= "true"/>

<supports-screens
Android:anydensity= "true"
Android:largescreens= "true"
Android:normalscreens= "true"
Android:smallscreens= "true"
Android:resizeable= "true"/>

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.