mobile phone equipment is too much and the resolution is different. Seeing most of the adaptive fonts on the Internet is to define Values320 × 480 or Value-HDPI to process it.
The first one is miserable. The resolution of many devices is different. Do you have to define each one?
uses the second one to have no effect in the tablet.
Last code is convenient and fast. Essence Essence
[java]
PUBLIC Static Void ChangeViewSize (ViewGroup ViewGroup, Int Screenwidth, Int Screenheight) {// Pass to the top layer Layout, screen width, 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) {// button to increase this must be placed on TextView, because Button has also inherited 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 the font size
public static int adjustFontSize(int screenWidth, int screenHeight) {
screenWidth=screenWidth>screenHeight?screenWidth:screenHeight;
/**
* 1. Get the view width in the ONSIZECHANGED of the view. Generally, the default width is 320, so calculate a zoom ratio
Rate = (Float) W/320 W is the actual width
2. Then Paint.SettextSize ((int) (8*Rate)) when setting font size;
Actual font size = default font size x rate
*/
int Rate = (int) (5*(float) screenwidth/320); // It is more suitable for me to test this multiple, of course, you can modify it before modifying it before modifying
RETURN RATE <15? 15: Rate; // The font is too small and not good -looking
}
// Traversing settings fonts
PUBLIC Static Void ChangeViewSize (ViewGroup ViewGroup, Int Screenwidth, Int Screenheight) {// Pass to the top of Activity, screen width, 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 (VINSTANCEOF BUTTON) {// button to increase this must be placed on TextView, because Button has also inherited 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 the font size
public static int adjustFontSize(int screenWidth, int screenHeight) {
screenWidth=screenWidth>screenHeight?screenWidth:screenHeight;
/**
* 1. Get the view width in the viewChanged in the view. Generally, the default width is 320, so calculate a zoom ratio
rate = (Float) W/320 W is the actual width
2. Then Paint.SettextSize ((int) (8*Rate)) when setting font size;
Actual font size = default font size x rate
*/
int Rate = (int) (5*(float) screenwidth/320); // I test this multiples by myself is more suitable, of course you can test it before modifying it before modifying
Return rate <15? 15: rate; // The font is too small and it doesn’t look good
}
Finally, just call the ChangeViewSize after the onCreate of Avtivity. Essence Essence The text is large, so its corresponding background is big, so the background picture of the control of the control is recommended to use the 9 -palace type picture, which looks comfortable.
Another additional, if you want to browse on a tablet computer, you are not hindered. Please add the manifest node (DTD is recommended to put it on the Application node) in the Androidmanifest.xml file:
[java]
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”/>