【问题标题】:Measure view size after screen rotation屏幕旋转后测量视图大小
【发布时间】:2015-04-02 02:46:28
【问题描述】:

我正在使用它来测量视图大小:

int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(toExpandVystrahy.getWidth(), View.MeasureSpec.EXACTLY);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
textVystrahy.measure(widthMeasureSpec, heightMeasureSpec);
int vystrahyHeight = textVystrahy.getMeasuredHeight();

我的onActivityCreated 内部片段中有这段代码。当我第一次启动我的应用程序时,它工作得很好并且可以正确测量。但是,当我更改屏幕方向并从片段内部的onActivityCreated 运行此方法时,它仍然返回与旋转前相同的值。那么屏幕旋转后我应该在哪里测量视图高度? onActivityCreated 位置不对吗?

感谢转发

【问题讨论】:

标签: android measure


【解决方案1】:

在您的自定义视图中覆盖 onMeasure 方法并使用 getMeasuredWidth() 和 getMeasuredHeight() 获取视图的大小

我做这样的事情。

    @Override
    protected void onMeasure(int width, int height) {
        super.onMeasure(width,height);
        int w = getMeasuredWidth();
        int h = getMeasuredHeight();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多