初始化我已經初始化代碼一個TextView(即不是從佈局初始化它)的getHeight()上一個TextView代碼
TextView v = new TextView(getApplicationContext());
然後,我設置文本
v.setText("Foo");
然後我試圖讓這個TextView的
int i = v.getHeight();
這是給0
的高度請指教
初始化我已經初始化代碼一個TextView(即不是從佈局初始化它)的getHeight()上一個TextView代碼
TextView v = new TextView(getApplicationContext());
然後,我設置文本
v.setText("Foo");
然後我試圖讓這個TextView的
int i = v.getHeight();
這是給0
的高度請指教
有可能您的TextView尚未測量。 隨着ViewTreeObserver你可以通知你的時候TextView的呈現,那麼你可以有它的高度:
ViewTreeObserver vto = v.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (v != null && getActivity() != null) {
ViewTreeObserver obs = v.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
int i = v.getHeight();
}
}
});
'的TextView V =新的TextView(ActivityName.this));'和v應該被添加到根佈局或到活動。並閱讀http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context雖然你的'v.getHeight()'是一個不同的問題 – Raghunandan
你剛剛創建了對象,度量和佈局傳遞發生了嗎?在那之前你如何獲得身高?將它添加到一些佈局,然後調用v.requestLayout(),然後調用layout.post(new Runnable(){public void run(){mHeight = v.getHeight}};) 。希望有所幫助。 – himanshurb
其實,在我的文章中缺少一條信息.......我將視圖添加到表格行,然後在TextView上調用getHeight() – EdmDroid