手机设备太多,分辨率也不同,看到网上大部分的适应字体的方法是定义values320×480或value-hdpi方式去处理。
采用第一种的就惨了,不少设备的分辨率是不同的,难道要每种都定义吗?
采用第二种的在平板电脑里没有效果。
最后仍是代码的方式方便快捷。。。
android
//遍历设置字体 测试
public static void changeViewSize(ViewGroup viewGroup,int screenWidth,int screenHeight) {//传入Activity顶层Layout,屏幕宽,屏幕高 字体
int adjustFontSize = adjustFontSize(screenWidth,screenHeight); spa
for(int i = 0; i<viewGroup.getChildCount(); i++ ){ orm
View v = viewGroup.getChildAt(i); xml
if(v instanceof ViewGroup){ 继承
changeViewSize((ViewGroup)v,screenWidth,screenHeight); 图片
}else if(v instanceof Button){//按钮加大这个必定要放在TextView上面,由于Button也继承了TextView 开发
( (Button)v ).setTextSize(adjustFontSize+2);
}else if(v instanceof TextView){
if(v.getId()== R.id.title_msg){//顶部标题
( (TextView)v ).setTextSize(adjustFontSize+4);
}else{
( (TextView)v ).setTextSize(adjustFontSize);
}
}
}
}
//获取字体大小
public static int adjustFontSize(int screenWidth, int screenHeight) {
screenWidth=screenWidth>screenHeight?screenWidth:screenHeight;
/**
* 1. 在视图的 onsizechanged里获取视图宽度,通常状况下默认宽度是320,因此计算一个缩放比率
rate = (float) w/320 w是实际宽度
2.而后在设置字体尺寸时 paint.setTextSize((int)(8*rate)); 8是在分辨率宽为320 下须要设置的字体大小
实际字体大小 = 默认字体大小 x rate
*/
int rate = (int)(5*(float) screenWidth/320); //我本身测试这个倍数比较适合,固然你能够测试后再修改
return rate<15?15:rate; //字体过小也很差看的
}
最后在Avtivity的oncreate完后调用一下changeViewSize就好了。。。文字大了那么它对应的背景也就跟着大,因此建议控件的背景图片用9宫格类型的图片,看起来舒服。
另外附加,若是你开发的应用想在平板电脑上浏览无碍请在AndroidManifest.xml文件中的manifest节点(DTD建议放在application节点上面)里加入:
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"/>