android根据分辨率自动调整字体大小的实例代码

命中猪ING

命中猪ING

2016-02-19 09:33

清醒时做事,糊涂时读书,大怒时睡觉,无聊时关注图老师为大家准备的精彩内容。下面为大家推荐android根据分辨率自动调整字体大小的实例代码,无聊中的都看过来。

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

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

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

[java]
代码如下:

//遍历设置字体 
public static void changeViewSize(ViewGroup viewGroup,int screenWidth,int screenHeight) {//传入Activity顶层Layout,屏幕宽,屏幕高 
        int adjustFontSize = adjustFontSize(screenWidth,screenHeight);
        for(int i = 0; iviewGroup.getChildCount(); i++ ){
            View v = viewGroup.getChildAt(i);
            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=screenWidthscreenHeight?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 rate15?15:rate; //字体太小也不好看的 
}

//遍历设置字体
public static void changeViewSize(ViewGroup viewGroup,int screenWidth,int screenHeight) {//传入Activity顶层Layout,屏幕宽,屏幕高
  int adjustFontSize = adjustFontSize(screenWidth,screenHeight);
  for(int i = 0; iviewGroup.getChildCount(); i++ ){
   View v = viewGroup.getChildAt(i);
   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=screenWidthscreenHeight?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 rate15?15:rate; //字体太小也不好看的
}

最后在Avtivity的oncreate完后调用一下changeViewSize就行了。。。文字大了那么它对应的背景也就跟着大,所以建议控件的背景图片用9宫格类型的图片,看起来舒服。
另外附加,如果你开发的应用想在平板电脑上浏览无碍请在AndroidManifest.xml文件中的manifest节点(DTD建议放在application节点上面)里加入:
[java]
代码如下:

supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true" 
        android:resizeable="true"/

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:resizeable="true"/

展开更多 50%)
分享

猜你喜欢

android根据分辨率自动调整字体大小的实例代码

编程语言 网络编程
android根据分辨率自动调整字体大小的实例代码

字体大师如何调整字体大小

手机软件 应用软件
字体大师如何调整字体大小

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

Excel怎么调整字体大小?

excel
Excel怎么调整字体大小?

美文FM如何调整字体大小

手机软件 应用软件
美文FM如何调整字体大小

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

当当读书怎么调整字体大小

手机软件 应用软件
当当读书怎么调整字体大小

安卓手机如何调整字体大小

Android
安卓手机如何调整字体大小

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

Android定时器实现的几种方式整理及removeCallbacks失效问题解决

Android定时器实现的几种方式整理及removeCallbacks失效问题解决

JavaScript Archive Network 集合

JavaScript Archive Network 集合
下拉加载更多内容 ↓