分享

Android下如何计算要显示的字符串所占的宽度和高度

 gearss 2016-12-23

查询了google发现在android一下有几种方法可以做到,但是经过实际测试发现下面这种方法是最准确的

复制代码
Rect bounds = new Rect();
String text = "Hello World";
TextPaint paint;

paint = findViewById(R.id.hello_world).getPaint();
paint.getTextBounds(text, 0, text.length(), bounds);
int width = bounds.width();
复制代码

 

Paint pFont = new Paint();
Rect rect = new Rect();
pFont.getTextBounds("豆", 0, 1, rect);
Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多