Imo*_*tep 40 android textview text-size android-layout
将textView放在指定中心的x和y坐标上时遇到问题.首先,我尝试在textView中设置文本,并使用此链接移动视图的宽度和高度 .
但它不起作用,我想尝试别的东西.我想知道是否有一种方法可以获得指定文本在textView中的大小?我的意思是,我知道文本和textSize,我如何获得textView将采用的宽度和高度?
(NSString)sizeWithFont;对于那些了解iPhone开发者的方法有点像.
小智 81
Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText().toString(), 0, textView.getText().length(), bounds);
Run Code Online (Sandbox Code Playgroud)
bounds.width() 将为您提供文本视图中文本的准确宽度.
She*_*tib 64
如果您的textview被称为电视
TV.setText("bla");
TV.measure(0, 0); //must call measure!
TV.getMeasuredHeight(); //get height
TV.getMeasuredWidth(); //get width
Run Code Online (Sandbox Code Playgroud)
更多相关信息(更新):如何获取视图的宽度/高度
Mar*_*l W 11
出于某种原因,Midverse Engineer的解决方案并没有给我总是正确的结果(至少在某些Android版本上).Sherif elKhatib的解决方案有效,但具有改变MeasuredWidth和Height的副作用.这可能导致textView的定位不正确.
我的解决方案
width = textView.getPaint().measureText(text);
metrics = textView.getPaint().getFontMetrics();
height = metrics.bottom - metrics.top;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42272 次 |
| 最近记录: |