Android 测量字符串在屏幕中的宽度以及两个空格的表示

Android 测量字符串在屏幕中的宽度以及两个空格的表示
1.两个空格

<string name="spaceTwo">&#12288;</string>

2.获取字符串在屏幕中的宽度

TextView content = findViewById(R.id.text_view);
Rect rect = new Rect();
String info = "Measure Text";
content.getPaint().getTextBounds(info,0,info.length(),rect);
//字符串在屏幕中的长度
int width = rect.width()

3.封装

/** * 测量字符串在屏幕中的宽高 rect.width() rect.height() * @param content * @param info * @return */
    public static Rect measureStringWidthOnScrren(TextView content,String info){
        Rect rect = new Rect();
        content.getPaint().getTextBounds(info,0,info.length(),rect);
        return rect;
    }
点赞