【发布时间】:2013-01-17 19:27:56
【问题描述】:
我正在尝试确定TextView 在绘制之前的高度。我正在使用以下代码来做到这一点:
TextView textView = (TextView) findViewById(R.id.textview);
textView.setText("TEST");
int widthSpec = MeasureSpec.makeMeasureSpec(LayoutParams.MATCH_PARENT, MeasureSpec.EXACTLY);
int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(widthSpec, heightSpec);
System.out.println("MeasuredHeight: " + textView.getMeasuredHeight());
输出为MeasuredHeight: 28。没有错。
但是,当我给TextView 一个长文本字符串时,会发生换行,它仍然给出单行的高度而不是两行:
(...)
textView.setText("TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST");
(...)
输出是MeasuredHeight: 28,我希望是MeasuredHeight: 56。
为什么它没有给我正确的价值,我怎样才能获得正确的价值?
【问题讨论】:
-
也许 [this][1] 会帮助你,这几乎是同样的问题! [1]:stackoverflow.com/questions/6157652/…
-
我已经在该线程和这个线程 (stackoverflow.com/questions/4668939/…) 中尝试了建议的解决方案,但结果相同。
-
您在哪里使用了该代码?
-
这是在
onCreate()方法中。 -
尝试在您的一个视图上发布(使用
post)Runnable(在onCreate方法中)并将您当前的代码放在那里。