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"/>