免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8516 | 回复: 2
打印 上一主题 下一主题

android字体大小根据分辨率自动调整 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-19 20:40 |只看该作者 |倒序浏览
android字体大小根据分辨率自动调整







手机设备太多,分辨率也不一样,看到网上大部分的适应字体的方法是定义values320×480或value-hdpi方式去处理。
采用第一种的就惨了,很多设备的分辨率是不一样的,难道要每种都定义吗?
采用第二种的在平板电脑里没有效果。

最后还是代码的方式方便快捷。。。


Java代码
  1. 1.//遍历设置字体   
  2. 2.public static void changeViewSize(ViewGroup viewGroup,int screenWidth,int screenHeight) {//传入Activity顶层Layout,屏幕宽,屏幕高   
  3. 3.        int adjustFontSize = adjustFontSize(screenWidth,screenHeight);   
  4. 4.        for(int i = 0; i<viewGroup.getChildCount(); i++ ){   
  5. 5.            View v = viewGroup.getChildAt(i);   
  6. 6.            if(v instanceof ViewGroup){   
  7. 7.                changeViewSize((ViewGroup)v,screenWidth,screenHeight);   
  8. 8.            }else if(v instanceof Button){//按钮加大这个一定要放在TextView上面,因为Button也继承了TextView   
  9. 9.                ( (Button)v ).setTextSize(adjustFontSize+2);   
  10. 10.            }else if(v instanceof TextView){   
  11. 11.                if(v.getId()== R.id.title_msg){//顶部标题   
  12. 12.                    ( (TextView)v ).setTextSize(adjustFontSize+4);   
  13. 13.                }else{   
  14. 14.                    ( (TextView)v ).setTextSize(adjustFontSize);   
  15. 15.                }   
  16. 16.            }   
  17. 17.        }   
  18. 18.    }   
  19. 19.  
  20. 20.  
  21. 21.//获取字体大小   
  22. 22.public static int adjustFontSize(int screenWidth, int screenHeight) {   
  23. 23.        screenWidth=screenWidth>screenHeight?screenWidth:screenHeight;   
  24. 24.        /**  
  25. 25.         * 1. 在视图的 onsizechanged里获取视图宽度,一般情况下默认宽度是320,所以计算一个缩放比率  
  26. 26.            rate = (float) w/320   w是实际宽度  
  27. 27.           2.然后在设置字体尺寸时 paint.setTextSize((int)(8*rate));   8是在分辨率宽为320 下需要设置的字体大小  
  28. 28.            实际字体大小 = 默认字体大小 x  rate  
  29. 29.         */  
  30. 30.        int rate = (int)(5*(float) screenWidth/320); //我自己测试这个倍数比较适合,当然你可以测试后再修改   
  31. 31.        return rate<15?15:rate; //字体太小也不好看的   
  32. 32.}  
  33. //遍历设置字体
  34. public static void changeViewSize(ViewGroup viewGroup,int screenWidth,int screenHeight) {//传入Activity顶层Layout,屏幕宽,屏幕高
  35.                 int adjustFontSize = adjustFontSize(screenWidth,screenHeight);
  36.                 for(int i = 0; i<viewGroup.getChildCount(); i++ ){
  37.                         View v = viewGroup.getChildAt(i);
  38.                         if(v instanceof ViewGroup){
  39.                                 changeViewSize((ViewGroup)v,screenWidth,screenHeight);
  40.                         }else if(v instanceof Button){//按钮加大这个一定要放在TextView上面,因为Button也继承了TextView
  41.                                 ( (Button)v ).setTextSize(adjustFontSize+2);
  42.                         }else if(v instanceof TextView){
  43.                                 if(v.getId()== R.id.title_msg){//顶部标题
  44.                                         ( (TextView)v ).setTextSize(adjustFontSize+4);
  45.                                 }else{
  46.                                         ( (TextView)v ).setTextSize(adjustFontSize);
  47.                                 }
  48.                         }
  49.                 }
  50.         }


  51. //获取字体大小
  52. public static int adjustFontSize(int screenWidth, int screenHeight) {
  53.                 screenWidth=screenWidth>screenHeight?screenWidth:screenHeight;
  54.                 /**
  55.                  * 1. 在视图的 onsizechanged里获取视图宽度,一般情况下默认宽度是320,所以计算一个缩放比率
  56.                            rate = (float) w/320   w是实际宽度
  57.                    2.然后在设置字体尺寸时 paint.setTextSize((int)(8*rate));   8是在分辨率宽为320 下需要设置的字体大小
  58.                           实际字体大小 = 默认字体大小 x  rate
  59.                  */
  60.                 int rate = (int)(5*(float) screenWidth/320); //我自己测试这个倍数比较适合,当然你可以测试后再修改
  61.                 return rate<15?15:rate; //字体太小也不好看的
  62. }
复制代码
最后在Avtivity的oncreate完后调用一下changeViewSize就行了。。。文字大了那么它对应的背景也就跟着大,所以建议控件的背景图片用9宫格类型的图片,看起来舒服。
另外附加,如果你开发的应用想在平板电脑上浏览无碍请在AndroidManifest.xml文件中的manifest节点(DTD建议放在application节点上面)里加入:


Java代码
  1. 1.<supports-screens   
  2. 2.        android:anyDensity="true"  
  3. 3.        android:largeScreens="true"  
  4. 4.        android:normalScreens="true"  
  5. 5.        android:smallScreens="true"   
  6. 6.        android:resizeable="true"/>  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-01-19 22:04 |只看该作者
谢谢分享

论坛徽章:
0
3 [报告]
发表于 2012-01-24 00:01 |只看该作者
谢谢分享。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP