Android font size is automatically adjusted based on resolution

Source: Internet
Author: User

There are too many mobile phone devices and the resolution is different. Most methods to adapt to the font on the Internet are to define the values320 × 480 or value-hdpi method for processing.
The first type is miserable. The resolutions of many devices are different. Do you need to define each type?
Using the second method has no effect on tablets.


Finally, the Code method is convenient and convenient...


[Java]
// Print and set the font
Public static void changeViewSize (ViewGroup viewGroup, int screenWidth, int screenHeight) {// input the top-layer Layout of the Activity, screen width, and screen height.
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) {// The Button must be added to 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 );
}
}
}
}
 
 
// Obtain the font size
Public static int adjustFontSize (int screenWidth, int screenHeight ){
ScreenWidth = screenWidth> screenHeight? ScreenWidth: screenHeight;
/**
* 1. Obtain the view width in onsizechanged of the view. The default width is 320. Therefore, a scaling ratio is calculated.
Rate = (float) w/320 w is the actual width
2. Then, when setting the font size, paint. setTextSize (int) (8 * rate); 8 is the font size to be set when the resolution width is 320
Actual font size = default font size x rate
*/
Int rate = (int) (5 * (float) screenWidth/320); // I can test this multiple by myself. Of course, you can test it before modifying it.
Return rate <15? 15: rate; // The font is too small to look good.
}

// Print and set the font
Public static void changeViewSize (ViewGroup viewGroup, int screenWidth, int screenHeight) {// input the top-layer Layout of the Activity, screen width, and screen height.
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) {// The Button must be added to 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 );
}
}
}
}


// Obtain the font size
Public static int adjustFontSize (int screenWidth, int screenHeight ){
ScreenWidth = screenWidth> screenHeight? ScreenWidth: screenHeight;
/**
* 1. Obtain the view width in onsizechanged of the view. The default width is 320. Therefore, a scaling ratio is calculated.
Rate = (float) w/320 w is the actual width
2. Then, when setting the font size, paint. setTextSize (int) (8 * rate); 8 is the font size to be set when the resolution width is 320
Actual font size = default font size x rate
*/
Int rate = (int) (5 * (float) screenWidth/320); // I can test this multiple by myself. Of course, you can test it before modifying it.
Return rate <15? 15: rate; // The font is too small to look good.
}
At last, call changeViewSize after oncreate of Avtivity... If the text size is big, the corresponding background will be big. Therefore, we recommend that you use the background image of the control with a 9-cell frame to make it look comfortable.
In addition, if your applications cannot be viewed on a tablet, add the following to the manifest node in the AndroidManifest. xml file (the DTD is recommended to be placed on the application node:
[Java]
<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.