我試圖確定繪製之前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
長文本字符串,所以包裝時,它仍然給一個單行的高度,而不是兩個:
(...)
textView.setText("TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST");
(...)
輸出是MeasuredHeight: 28
,在那裏我會想到MeasuredHeight: 56
。
爲什麼它不給我正確的價值,我如何才能達到正確的價值?
設置高度也許[這裏] [1]將幫助你,這是幾乎同樣的問題! [1]:http://stackoverflow.com/questions/6157652/android-getmeasuredheight-returns-wrong-values – ObAt
我試過在該線程,並在這一個提議的解決方案(HTTP://計算器.com/questions/4668939/viewgrouptextview-getmeasuredheight-giving-wrong-value-is-smaller-real-real),但它們具有相同的結果。 – nhaarman
你究竟在哪裏使用該代碼? – Luksprog