Android:获取需要显示的字符串所占的宽度和高度

Rect bounds = new Rect();
String text = "测试";
TextPaint paint;
paint = findViewById(R.id.hello_world).getPaint();
paint.getTextBounds(text, 0, text.length(), bounds);
int height = getStringHeight = (text, paint)




private int getStringHeight(String string, Paint paint) {
        Rect rect = new Rect();
        paint.getTextBounds(string, 0, string.length(), rect);
        return rect.height();
    }
 
    private int getStringWidth(String string, Paint paint) {
        Rect rect = new Rect();
        paint.getTextBounds(string, 0, string.length(), rect);
        return rect.width();
    }
发布了154 篇原创文章 · 获赞 391 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/LucasXu01/article/details/102723058
今日推荐