打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android 计算字符串在屏幕上所占的长度和宽度(像素单位)
获取字符串占的屏幕的宽度:
[java] view plain copy
Paint mPaint = new Paint();
int width = mPaint.measureText(hello);  //在屏幕中所占的宽度
计算出当前绘制出来的字符串有多宽,可以这么来!
方法1:
[java] view plain copy
Paint pFont = new Paint();
Rect rect = new Rect();
//返回包围整个字符串的最小的一个Rect区域
pFont.getTextBounds(str, 0, 1, rect);
strwid = rect.width();
strhei = rect.height();
方法2:
[java] view plain copy
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();
方法3:
[java] view plain copy
Paint pFont = new Paint();
Rect rect = new Rect();
pFont.getTextBounds("豆", 0, 1, rect);
Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android下如何计算要显示的字符串所占的宽度和高度
计算手机屏幕的长度和宽度
Android开发:最全面、最易懂的Android屏幕适配解决方案
设置 viewport 实现定宽网页 WebApp 下布局自适应
移动端布局
ios 获取屏幕的属性和宽度
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服