您的位置:首页 > 其它

安卓获取TextView中字符串占屏幕宽度的方法

2014-08-19 13:27 246 查看
public static int getLineLength(String str, float textSize) {

Paint pFont = new Paint();

pFont.setTextSize(textSize);

return (int) pFont.measureText(str);

}



str为TextView获取的字符串,textSize为字符串的字符的字号,单位是像素(px),

返回值的单位也是px.

可以使用

public static int dip2px(Context context, float dpValue) {

final float scale = context.getResources().getDisplayMetrics().density;

return (int) (dpValue * scale + 0.5f);

}

public static int px2dip(Context context, float pxValue) {

final float scale = context.getResources().getDisplayMetrics().density;

return (int) (pxValue / scale + 0.5f);

}

这两个方法转换dp 和px 单位,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: