时间:2021-05-20
手机设备太多,分辨率也不一样,看到网上大部分的适应字体的方法是定义values320×480或value-hdpi方式去处理。
采用第一种的就惨了,很多设备的分辨率是不一样的,难道要每种都定义吗?
采用第二种的在平板电脑里没有效果。
最后还是代码的方式方便快捷。。。
[java]
复制代码 代码如下:
//遍历设置字体
public static void changeViewSize(ViewGroup viewGroup,int screenWidth,int screenHeight) {//传入Activity顶层Layout,屏幕宽,屏幕高
int adjustFontSize = adjustFontSize(screenWidth,screenHeight);
for(int i = 0; i<viewGroup.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=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; //字体太小也不好看的
}
//遍历设置字体
public static void changeViewSize(ViewGroup viewGroup,int screenWidth,int screenHeight) {//传入Activity顶层Layout,屏幕宽,屏幕高
int adjustFontSize = adjustFontSize(screenWidth,screenHeight);
for(int i = 0; i<viewGroup.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=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节点上面)里加入:
[java]
复制代码 代码如下:
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"/>
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"/>
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了jQuery实现自动调整字体大小的方法。分享给大家供大家参考。具体分析如下:这里使用一个jQuery函数,自动更改元素中的文本的字体大小。$.fn
mac调整字体大小教程来咯~大家可以参考下文步骤进行mac系统字体大小调整步骤,Mac系统是可以调整字体大小的哦~具体该如何操作呢?有需要的朋友请参考下文步
mac分辨率设置教程来咯~不知道mac调整分辨率步骤的朋友可以参考一下,有些朋友不习惯macos系统字体大小,该如何更改分辨率呢?希望下文的方法能够帮助到大
word正文字体大小调整可直接在开始页面的字体中根据自己的需要调整字体大小和文字字体。 MicrosoftOfficeWord是微软公司的一个文字处理器应用程
Powerpoint录入文本,会根据文本框的大小,自动调整字体大小。对于某些特定ppt编辑者,会希望关闭此功能。此功能可在“自动更正”选