TextView-- 测量文字宽度
?https://my.oschina.net/lengwei/blog/637380;
http://blog.csdn.net/mare_blue/article/details/51388403;
http://blog.csdn.net/baidu_31093133/article/details/52413893;
?
--1,Android中調(diào)用Paint的measureText()方法取得字符串顯示的寬度值:
public static float GetTextWidth(String text, float Size) {
//第一個(gè)參數(shù)是要計(jì)算的字符串,第二個(gè)參數(shù)是字提大小
2 TextPaint FontPaint = new TextPaint();
3 FontPaint.setTextSize(Size);
4 return FontPaint.measureText(text);
5 }
?
?
有時(shí)候因?yàn)楣ぷ餍枰?#xff0c;我們需要獲得android文字中的寬度。
如下:
textView = (TextView) findViewById(R.id.tv_content);?
textView.setTextSize(16);
Log.e(TAG, textView.getPaint().measureText(textView.getText().toString())+"");
textView.setTextSize(20);
Log.e(TAG, textView.getPaint().measureText(textView.getText().toString())+"");
可以發(fā)現(xiàn)設(shè)置不同字體大小時(shí),寬度也是不一樣的,輸出結(jié)果如下:
與字體大小成正比。
轉(zhuǎn)載于:https://www.cnblogs.com/awkflf11/p/6344760.html
總結(jié)
以上是生活随笔為你收集整理的TextView-- 测量文字宽度的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何用CSS快速布局(一)—— 布局元素
- 下一篇: codevs1258 关路灯(☆区间dp