TextView
の高さを決定する前に、が描画されています。私はそれを行うには、次のコードを使用しています:ラップされたテキストを含むTextViewのgetMeasuredHeight()
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
長いテキスト文字列を与えるので、ラッピングが発生したときに
しかし、それはまだ一行の高さの代わりに、2を与える:
(...)
textView.setText("TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST");
(...)
出力は、私がMeasuredHeight: 56
を期待MeasuredHeight: 28
、あります。
なぜ私に適切な価値が与えられず、正しい価値を達成するにはどうすればよいですか?
で高さを設定するgetActual高さ関数を使用する必要があります! [1]:http://stackoverflow.com/questions/6157652/android-getmeasuredheight-returns-wrong-values – ObAt
私はそのスレッドとこの1つで提案されたソリューションを試しました(http:// stackoverflow .com/questions/4668939/viewgrouptextview-getmeasuredheight-wrong-value-is-real-than)は、同じ結果を示します。 – nhaarman
あなたはそのコードを正確にどこで使いましたか? – Luksprog