gpt4 book ai didi

java - 如何获取稍后使用 addView() 填充的 Linearlayout 的最终高度

转载 作者:太空宇宙 更新时间:2023-11-04 14:42:01 30 4
gpt4 key购买 nike

我有一个 LinearLayout,我使用 addview() 填充了其他 View ;但是当我尝试获取线性布局的高度时,它返回 0。

我使用了((LinearLayout)findViewById(R.id. LinearId)).getHeight();

我添加的 View ,还有 subview 。我如何获得包括所有子项在内的 Linearlayout 的高度?

编辑-

我用来将 View 添加到线性布局的实际代码 -

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 后的堆栈跟踪 -

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)

最佳答案

将子级添加到 LinearLayout 后,它不会立即执行测量和布局,因此此时它的大小仍然未知。您需要等待布局阶段才能知道尺寸。
您可以通过子类化 LinearLayout 并在 onSizeChanged 方法等中读取它的大小来实现。
或者您可以通过在 ViewTreeObserver 上设置 OnGlobalLayoutListener 来实现它,而无需子类化 LinearLayout:

myLinearLayout = ((LinearLayout)findViewById(R.id.linearId);

然后在添加 View 之前添加一个 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);

关于java - 如何获取稍后使用 addView() 填充的 Linearlayout 的最终高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24842143/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com