错误的观点高度/上方向的改变宽度值? [英] Wrong View Height/Width value on Orientation change?

查看:196
本文介绍了错误的观点高度/上方向的改变宽度值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现onConfigurationChnage(..)读取的观点高度值和宽度方向上配置chnage

I implemented onConfigurationChnage(..) to read value of view height and width on orientation configuration chnage

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int height = displaymetrics.heightPixels;
        int width = displaymetrics.widthPixels;

        Log.i(TAG, "onConfigurationChanged: "+newConfig.orientation+", "+height+", "+width + ", "+mTempeFrameLayout.getWidth());
    }

但它总是回复旧值,如果方向得到改变它给了最后的方位(可能)值。

but it always reply old value, if orientation get change it gave last orientation (probably)value.

屏幕宽度/高度使用的 displaymetrics 是未来的权利正常,但同样是不工作的任何看法。

Screen width/height using displaymetrics is coming right properly but same is not working for any view.

任何一个有idea..how可以获取方向更改正确的值?

Any one have idea..how can get correct value on orientation Change?

推荐答案

getWidth()返回的宽度作为视图的布局,这意味着你需要等待,直到它被绘制到屏幕上。 onConfigurationChanged被称为重绘视图的新配置之前,所以我不认为你就可以得到你的新宽度,直到后来。

getWidth() returns the width as the View is laid out, meaning you need to wait until it is drawn to the screen. onConfigurationChanged is called before redrawing the view for the new configuration and so I don't think you'll be able to get your new width until later.

@Override
public void onConfigurationChanged(Configuration newConfiguration) {
    super.onConfigurationChanged(newConfiguration);
    final View view = findViewById(R.id.scrollview);

    ViewTreeObserver observer = view.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            Log.v(TAG,
                    String.format("new width=%d; new height=%d", view.getWidth(),
                            view.getHeight()));
            view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
}

这篇关于错误的观点高度/上方向的改变宽度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆