屏幕旋转后测量视图大小

时间:2014-12-09 19:16:24

标签: android measure

我用它来测量视图大小:

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正确的地方吗?

先谢谢

1 个答案:

答案 0 :(得分:0)

在自定义视图中覆盖onMeasure方法,并使用getMeasuredWidth()和getMeasuredHeight()

获取View的大小

我这样做。

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