gpt4 book ai didi

android - 将屏幕方向锁定为其(自然)默认方向

转载 作者:太空狗 更新时间:2023-10-29 14:28:18 29 4
gpt4 key购买 nike

我想将屏幕方向锁定为其默认方向。我在实现这一点时遇到问题。最初我将屏幕锁定为 list 中的肖像。它适用于纵向默认设备。但是许多平板电脑默认为横向,因此在这些设备中锁定纵向是不合适的,我想检测此默认方向并将其锁定。我的意思是,如果横向是默认方向,我想将方向锁定为横向,如果是纵向,则将其锁定到端口。这该怎么做。我被困在这部分。我不想支持两个方向(自动)。请帮忙

谢谢。

最佳答案

不同设备有默认方向,例如 galaxy 10 平板电脑的默认方向不同于 nexus 7 平板电脑。当您获得显示器的方向时,您将获得以下值:

enter image description here

这么说,你在你的锁定方法中要做的是:

public void mLockScreenRotation(FA_Padre actividad){
int buildVersionSDK = Build.VERSION.SDK_INT;
Display display = ((WindowManager) actividad.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int orientation=0;
if(buildVersionSDK >= 8){
orientation=display.getRotation();
}
/****************Phone*************************************/
if(buildVersionSDK < 8){// older Android versions with only two orientations
switch (actividad.getResources().getConfiguration().orientation){

case Configuration.ORIENTATION_PORTRAIT:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Configuration.ORIENTATION_LANDSCAPE:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;

}
}else if((buildVersionSDK > 7 ) && (!GlobalInfo.isTablet())){// Newer Android phones with more than two orientations

switch(orientation){

case Surface.ROTATION_0:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_270:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;

}
/****************TABLET*************************************/
}else{
int width = 0;
int height = 0;
switch(orientation){
/*If the default orientation of the device is landscape Rotation_0 and rotation_180 will be the case if the device is being held in landscape. if the default orientation of the device is portrait rotation_0 or 180 will only be the case if the device is in portrait mode*/
case Surface.ROTATION_0:
case Surface.ROTATION_180:
width = display.getWidth();
height = display.getHeight();
break;
/*the opposite in here*/
case Surface.ROTATION_90: //
case Surface.ROTATION_270:
width = display.getHeight();
height = display.getWidth();
break;
default:
break;
}
if(width > height){//Default ORIENTATION = LANDSCAPE, lock the screen in the current orientation
switch(orientation){

case Surface.ROTATION_0:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_90:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_180:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_270:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}

} else {//Default ORIENTATION = PORTRAIT, lock the screen in the current orientation
switch(orientation){

case Surface.ROTATION_0:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
}

}

}
}

关于android - 将屏幕方向锁定为其(自然)默认方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9643844/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com