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