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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章