程序设置为横屏:
android:screenOrientation="landscape"希望获得屏幕宽高
@Override
public void onWindowFocusChanged(boolean hasFocus) {
height = rl.getHeight();
width = rl.getWidth();
logCat("rl.getHeight() " + height + "rl.getWidth() " + width );
super.onWindowFocusChanged(hasFocus);
} 上面代码在大部分手机正常
但是少部分手机,锁屏后再解锁,就会发现宽高反过来了。
所以要改成这样
static boolean m_bIs1stFocus=true;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if( m_bIs1stFocus ){
m_bIs1stFocus=false;
height = rl.getHeight();
width = rl.getWidth();
logCat("rl.getHeight() " + height + "rl.getWidth() " + width + " hasFocus:" + hasFocus);
}
super.onWindowFocusChanged(hasFocus);
}
本文介绍了一种解决横屏应用程序中屏幕宽度和高度获取不准确的问题的方法。通过改进 onWindowFocusChanged 方法中的逻辑,确保了即使在锁屏后再解锁的情况下也能正确获取到屏幕的宽度和高度。
1157

被折叠的 条评论
为什么被折叠?



