手機設備太多,分辨率也不同,看到網上大部分的適應字體的方法是定義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"/>