android - 获取 TextView 中文本的大小

标签 android android-layout textview text-size

我在将 textView 放置在指定中心的 x 和 y 坐标时遇到问题。 首先,我尝试在 textView 中设置文本,并以 View 的宽度和高度移动 View from this link .

但它不起作用,我想尝试其他方法。 我想知道是否有一种方法可以获取指定文本在我的 textView 中的大小? 我的意思是,我知道 text 和 textSize,如何获得 textView 的宽度和高度?

类似于 (NSString)sizeWithFont; 的方法,适合那些了解 iPhone 开发的人。

最佳答案

Rect bounds = new Rect();

textView.getPaint().getTextBounds(textView.getText().toString(), 0, textView.getText().length(), bounds);

bounds.width() 将为您提供 TextView 中文本的准确宽度。

关于android - 获取 TextView 中文本的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6734301/

相关文章:

android - 如何在 Android 中将 onClickListener 设置为整个屏幕?

Android 和 TextView 中的

android - 以编程方式从应用程序启动 Skype 并传递号码 - Android

android-layout - 如何在Android 4.0中全屏显示

android - AltBeacon 库 - 减少蓝牙扫描周期

android - Material 对话框中自定义布局的默认填充

android - 我可以在其中动态创建带有 RadioButtons 的 RadioGroup(没有 xml)吗?

Android TextView 的下标被剪掉

android - 在主线程(UI Thread)上监听UI事件以及触发事件的 View

java - 如何以用户友好的方式显示 Android 按钮?