【发布时间】:2014-06-30 01:21:55
【问题描述】:
我需要在动态添加项目后(膨胀后)测量线性布局。我在onStart 和onCreate 中尝试了getHeight 和getMeasuredHeight,但它们返回0。我能想到的一种解决方案是测量充气物品的高度,然后添加它们。但我希望有一个更合法的解决方案。
谢谢
【问题讨论】:
标签: android height android-linearlayout
我需要在动态添加项目后(膨胀后)测量线性布局。我在onStart 和onCreate 中尝试了getHeight 和getMeasuredHeight,但它们返回0。我能想到的一种解决方案是测量充气物品的高度,然后添加它们。但我希望有一个更合法的解决方案。
谢谢
【问题讨论】:
标签: android height android-linearlayout
在 Honeycomb 及更高版本上,您可以将 OnLayoutChangeListener 添加到视图中。
在 Honeycomb 之前,您必须使用 OnGlobalLayoutListener。这有点棘手,因为它会被整个布局调用,而不仅仅是你指定的视图,所以通常你会注册它,然后在第一次调用它时取消注册。
linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// do the rest of your stuff here
}
});
在测量/布局通过之前,报告的视图大小将为零。
【讨论】:
你必须等到它被绘制出来。您可以在根的布局中发布一个可运行文件,当它被调用时,您应该能够检索孩子的身高/体重
【讨论】: