Android 获取TextView 显示的字符串宽度
2016-08-17 16:52
501 查看
工作上有业务需要判断textview是否换行,我的做法是判断textview要显示的字符串的宽度是否超过我设定的宽度,若超过则会执行换行。
项目中的其他地方也有这样的需求,故直接使用了那一块的代码。如下
这里是使用了TextPaint的measureText方法。
不过在项目实践上发现了这个方法存在一些问题。当字符串存在字母数字时,就会有1-2像素的误差。也正是这个误差,导致代码上判断换行错误,使得界面上显示出错。
为了解决这个问题,搜到了这篇文章 戳我
这篇文章中使用了另外一个方法测量,没有new TextPaint,而是使用了TextView自己的TextPaint,这个Paint通过 TextView.getPaint() 方法获得。
最后给出一个例子来看这两种方法的差别。
测试机是MI4,xxdpi
代码如下
当测试字符串为: “测试中文”时,结果如下
当测试字符串为: “测试英文abcd”时,
可见使用textView的TextPaint调用measureText方法得到的宽度才是真正的宽度。
项目中的其他地方也有这样的需求,故直接使用了那一块的代码。如下
public float getTextWidth(Context Context, String text, int textSize){ TextPaint paint = new TextPaint(); float scaledDensity = Context.getResource().getDisplayMetrics().scaledDensity; paint.setTextSize(scaledDensity * textSize); return paint.measureText(text); }
这里是使用了TextPaint的measureText方法。
不过在项目实践上发现了这个方法存在一些问题。当字符串存在字母数字时,就会有1-2像素的误差。也正是这个误差,导致代码上判断换行错误,使得界面上显示出错。
为了解决这个问题,搜到了这篇文章 戳我
这篇文章中使用了另外一个方法测量,没有new TextPaint,而是使用了TextView自己的TextPaint,这个Paint通过 TextView.getPaint() 方法获得。
最后给出一个例子来看这两种方法的差别。
测试机是MI4,xxdpi
代码如下
public class MainActivity extends Activity { private final static String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 测试字符串 // 测试例子均用15sp的字体大小 String text = "测试中文"; TextView textView = (TextView) findViewById(R.id.test); textView.setText(text); int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); textView.measure(spec, spec); // getMeasuredWidth int measuredWidth = textView.getMeasuredWidth(); // new textpaint measureText TextPaint newPaint = new TextPaint(); float textSize = getResources().getDisplayMetrics().scaledDensity * 15; newPaint.setTextSize(textSize); float newPaintWidth = newPaint.measureText(text); // textView getPaint measureText TextPaint textPaint = textView.getPaint(); float textPaintWidth = textPaint.measureText(text); Log.i(TAG, "测试字符串:" + text); Log.i(TAG, "getMeasuredWidth:" + measuredWidth); Log.i(TAG, "newPaint measureText:" + newPaintWidth); Log.i(TAG, "textView getPaint measureText:" + textPaintWidth); } }
当测试字符串为: “测试中文”时,结果如下
测试字符串:测试中文 getMeasuredWidth:180 measureText:180.0 getPaint measureText:180.0
当测试字符串为: “测试英文abcd”时,
测试字符串:测试英文abcd getMeasuredWidth:279 newPaint measureText:278.0 textView getPaint measureText:279.0
可见使用textView的TextPaint调用measureText方法得到的宽度才是真正的宽度。
相关文章推荐
- Android 获取 TextView 显示的字符串宽度
- Android获取TextView显示的字符串宽度
- TextVie获取显示字符串的宽度之Android开发
- 【Android】 textview 中超出屏幕宽度的字符 省略号显示
- android textview 中超出屏幕宽度的字符 省略号显示
- 【Android】如何获取TextView的控件宽度以及字体宽度
- android textview 中超出屏幕宽度的字符 省略号显示
- android LocationManager的简单使用(获取经纬度信息到textview显示)
- Android控件TextView中ellipsize属性(设置当文字长度超过textview宽度时的显示方式)
- Android 获取TextView中text的宽度
- android 获取网络数据,回传到本地用TextView显示乱码问题解决方法
- 获取Textview的宽度 (在没有显示前)
- 安卓获取TextView中字符串占屏幕宽度的方法
- Android基础教程——在TextView中显示Html 自定义标签,获取标签属性
- 程序中动态更改TextView显示内容并获取res的字符串
- android textview 中超出屏幕宽度的字符 省略号显示
- Android获取TextView文本实际宽度
- android定位已获取经纬度,上传google解析后textview不显示,基础问题,求大神解答~~>_<`
- android textview 中超出屏幕宽度的字符 省略号显示
- android TextView 如何动态获取宽度