Android获取字体高度和设置行高
2014-03-27 19:22
337 查看
网络中好多资料介绍获取字体高度的方法如下:
Java代码
public int getFontHeight(float fontSize)
{
Paint paint = new Paint();
paint.setTextSize(fontSize);
FontMetrics fm = paint.getFontMetrics();
return (int) Math.ceil(fm.descent - fm.top) + 2;
}
个人更倾向于以下方式获取字体实际高度:
Java代码
Math.ceil(fm.descent - fm.ascent)
通过实际的截图对文字高度的确定,后者更准确一些。
有了字体高度信息,就可以添加行与行之间的空隙,调整行高。
个人实现方式如下:
Java代码
paint.setTextSize(fFontWidth);
FontMetrics fm = paint.getFontMetrics();
fFontHeight = (float)Math.ceil(fm.descent - fm.ascent);
if(fFontHeight > fLineHeight)
{
fMulValue = fLineHeight / fFontHeight;
fAddValue = -1;
}
else
{
fMulValue = 1;
fAddValue = fLineHeight - fFontHeight;
}
textViewLeft.setTextSize(fFontWidth);
textViewLeft.setLineSpacing(fAddValue, fMulValue);
实践验证这种方式对多种分辨率的屏幕的适应性较强。
以上是个人在实际的功能实现过程中的总结,水平较浅,若看官有不同的看法或者见解,望不惜赐教!
Java代码
public int getFontHeight(float fontSize)
{
Paint paint = new Paint();
paint.setTextSize(fontSize);
FontMetrics fm = paint.getFontMetrics();
return (int) Math.ceil(fm.descent - fm.top) + 2;
}
个人更倾向于以下方式获取字体实际高度:
Java代码
Math.ceil(fm.descent - fm.ascent)
通过实际的截图对文字高度的确定,后者更准确一些。
有了字体高度信息,就可以添加行与行之间的空隙,调整行高。
个人实现方式如下:
Java代码
paint.setTextSize(fFontWidth);
FontMetrics fm = paint.getFontMetrics();
fFontHeight = (float)Math.ceil(fm.descent - fm.ascent);
if(fFontHeight > fLineHeight)
{
fMulValue = fLineHeight / fFontHeight;
fAddValue = -1;
}
else
{
fMulValue = 1;
fAddValue = fLineHeight - fFontHeight;
}
textViewLeft.setTextSize(fFontWidth);
textViewLeft.setLineSpacing(fAddValue, fMulValue);
实践验证这种方式对多种分辨率的屏幕的适应性较强。
以上是个人在实际的功能实现过程中的总结,水平较浅,若看官有不同的看法或者见解,望不惜赐教!
相关文章推荐
- Android获取字体高度和设置行高
- Android获取字体高度和设置行高
- Android根据所设置的textSize获取字体高度和设置行高
- android 字符串 转换成Bitmap 设置文本的字体: 宋体,隶书啥的,获取字体的 行高, 文字生成图片
- Android GridView设置行高,即item高度
- 【Android】ListView监听上下滑动(设置滚动监听判断ListView的滚动方向同时获取屏幕高度、ListView实际高度,判断是否需要展示返回顶部按钮(具体逻辑请看代码--附有详细注释)。
- Android 获取ListView的高度,设置布局内控件超出布局部分可以显示
- Android 获取屏幕宽高 动态设置组件高度
- android 系统版本信息工具类 获取设备型号 设置屏幕亮度 软键盘 获取屏幕高度宽度
- android根据字体大小设置控件高度
- android 动态设置获取控件大小,并设置宽度等于高度,或者高度等于宽度。
- Android Chromium:成功地获取到用户自定义的系统默认字体设置
- android解决无法设定listview的item高度(设置行高)
- android 获取webView高度,设置webView高度
- Android获取状态栏高度,动态设置控件高度
- android 获取标题栏状态栏高度
- Android 注册页面 设置显示或者隐藏密码 和 点击获取验证码倒计时60s
- android 获取和设置屏幕亮度
- android textview设置字体的行距和字间距
- android 获取手机屏幕分辨率和setRequestedOrientation设置屏幕方向