【问题标题】:How to get the current height of a LinearLayout after addView?addView后如何获取LinearLayout的当前高度?
【发布时间】:2014-06-30 01:21:55
【问题描述】:

我需要在动态添加项目后(膨胀后)测量线性布局。我在onStartonCreate 中尝试了getHeightgetMeasuredHeight,但它们返回0。我能想到的一种解决方案是测量充气物品的高度,然后添加它们。但我希望有一个更合法的解决方案。

谢谢

【问题讨论】:

    标签: android height android-linearlayout


    【解决方案1】:

    在 Honeycomb 及更高版本上,您可以将 OnLayoutChangeListener 添加到视图中。

    在 Honeycomb 之前,您必须使用 OnGlobalLayoutListener。这有点棘手,因为它会被整个布局调用,而不仅仅是你指定的视图,所以通常你会注册它,然后在第一次调用它时取消注册。

    linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            // do the rest of your stuff here
        }
    });
    

    在测量/布局通过之前,报告的视图大小将为零。

    【讨论】:

      【解决方案2】:

      你必须等到它被绘制出来。您可以在根的布局中发布一个可运行文件,当它被调用时,您应该能够检索孩子的身高/体重

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-18
        • 1970-01-01
        • 1970-01-01
        • 2012-05-22
        • 1970-01-01
        • 2020-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多