美文网首页
TextView 计算文字宽高

TextView 计算文字宽高

作者: 程树欣 | 来源:发表于2016-12-21 16:23 被阅读154次

    有时候需要在 程序中计算TextView文本的宽高来动态设置一些数值,在Android中可以使用Paint开计算

    TextView tv = new TextView(context);
    tv.setTextSize(30);
    String str = "计算";
    Rect mr = new Rect();
    tv.getPaint().getTextBounds(str, 0,str.length(),mr);
    

    获取到Rect之后就能获取想要的数值了

    相关文章

      网友评论

          本文标题:TextView 计算文字宽高

          本文链接:https://www.haomeiwen.com/subject/gxvqvttx.html