我有一个LinearLayout,我使用addview()填充其他视图;但是当我试图获得线性布局的高度时,它返回0。
我使用了((LinearLayout)findViewById(R.id.linearId)).getHeight();
我正在添加视图,也有子视图。如何获得Linearlayout的高度,包括所有孩子?
编辑 -
用于将视图添加到线性布局的实际代码 -
final ViewTreeObserver viewTreeObserver = earningsContainer.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
// read height with myLinearLayout.getHeight() etc.
Log.e(TAG, "$$$$$height "+earningsContainer.getHeight());
// remember to remove the listener if possible
if (viewTreeObserver.isAlive()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
viewTreeObserver.removeOnGlobalLayoutListener(this);
} else {
viewTreeObserver.removeGlobalOnLayoutListener(this);
}
}
}
});
earningsContainer.addView(getRowTaxEarningsTitle());
for (int i = 0; i < taxMainObject.getTaxEarnings().size(); i++) {
earningsContainer.addView(getTaxEarningsRow(taxMainObject
.getTaxEarnings().get(i)));
totalEarnings += taxMainObject.getTaxEarnings().get(i)
.getTotalAmount();
}
earningsContainer.addView(getTaxTotalRow("GROSS TOTAL SALARY",
totalEarnings + ""));
使用ViewTreeObserver后的Stacktrace -
07-18 13:40:59.936: E/AndroidRuntime(10058): FATAL EXCEPTION: main
07-18 13:40:59.936: E/AndroidRuntime(10058): Process: com.synthesize.paysal, PID: 10058
07-18 13:40:59.936: E/AndroidRuntime(10058): java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewTreeObserver.checkIsAlive(ViewTreeObserver.java:720)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewTreeObserver.removeOnGlobalLayoutListener(ViewTreeObserver.java:529)
07-18 13:40:59.936: E/AndroidRuntime(10058): at com.synthesize.paysal.TaxinfoActivity$1.onGlobalLayout(TaxinfoActivity.java:125)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:815)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1782)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.Choreographer.doFrame(Choreographer.java:544)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.os.Handler.handleCallback(Handler.java:733)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.os.Handler.dispatchMessage(Handler.java:95)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.os.Looper.loop(Looper.java:136)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-18 13:40:59.936: E/AndroidRuntime(10058): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 13:40:59.936: E/AndroidRuntime(10058): at java.lang.reflect.Method.invoke(Method.java:515)
07-18 13:40:59.936: E/AndroidRuntime(10058): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-18 13:40:59.936: E/AndroidRuntime(10058): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-18 13:40:59.936: E/AndroidRuntime(10058): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
将孩子添加到LinearLayout
后,它不会立即执行测量和布局,因此此时它的大小仍然未知。您需要等待布局阶段才能知道尺寸
您可以通过继承LinearLayout
并在onSizeChanged方法等中读取它的大小来实现。
或者您可以通过在LinearLayout
上设置OnGlobalLayoutListener
而无需继承ViewTreeObserver
来实现它:
myLinearLayout = ((LinearLayout)findViewById(R.id.linearId);
然后在添加视图之前添加OnGlobalLayoutListener
:
myLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
// read height with myLinearLayout.getHeight() etc.
// remember to remove the listener if possible
ViewTreeObserver viewTreeObserver = myLinearLayout.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
viewTreeObserver.removeOnGlobalLayoutListener(this);
} else {
viewTreeObserver.removeGlobalOnLayoutListener(this);
}
}
}
});
mLinearLayout.addView(view);