android 小技术点汇总

来源:互联网 发布:优化驱动器一共几遍 编辑:程序博客网 时间:2024/05/12 22:18

1. 如何获取字符串的长度

方法一:

Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  mTextPaint.setColor(Color.WHITE);  // Define the string.  String displayText = “Hello World!”;  // 直接返回参数字符串所占用的宽度  float textWidth = mTextPaint.measureText(displayText); 


方法二:

Paint pFont = new Paint(); 
Rect rect = new Rect();
//返回包围整个字符串的最小的一个Rect区域
pFont.getTextBounds(str, 0, 1, rect); 
strwid = rect.width();

strhei = rect.height();


注意:

记得设字体的大小哦,不然取出的大小可能跟实际需要有偏差~









0 0