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中調用Paint的measureText()方法取得字符串顯示的寬度值:
public static float GetTextWidth(String text, float Size) {
//第一個參數是要計算的字符串,第二個參數是字提大小
2 TextPaint FontPaint = new TextPaint();
3 FontPaint.setTextSize(Size);
4 return FontPaint.measureText(text);
5 }
?
?
有時候因為工作需要,我們需要獲得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())+"");
可以發現設置不同字體大小時,寬度也是不一樣的,輸出結果如下:
與字體大小成正比。
轉載于:https://www.cnblogs.com/awkflf11/p/6344760.html
總結
以上是生活随笔為你收集整理的TextView-- 测量文字宽度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何用CSS快速布局(一)—— 布局元素
- 下一篇: codevs1258 关路灯(☆区间dp