我有以下代码.
Java的
public void lockScreenOrientation() { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); } public void unlockScreenOrientation() { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); }
我从javascript调用这些函数.控制正在进入这些方法.但是方向未锁定.
我试过跟随锁定方向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
这些似乎都不起作用.任何指针都会有所帮助.
解决方法
我已经创建了一些实用方法来帮助处理方向锁定,随意使用这个类.
使用示例:
>在活动中:OrientationUtils.lockOrientationPortrait(MyActivityName.this)
>在片段中:OrientationUtils.lockOrientationLandscape(getActivity())
码:
/** Static methods related to device orientation. */ public class OrientationUtils { private OrientationUtils() {} /** Locks the device window in landscape mode. */ public static void lockOrientationLandscape(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } /** Locks the device window in portrait mode. */ public static void lockOrientationPortrait(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } /** Allows user to freely use portrait or landscape mode. */ public static void unlockOrientation(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); } }
这是我在GitHub上的完整OrientationUtils类,可以在任何Android应用程序中使用:https://github.com/danialgoodwin/android-simply-advanced-helper/blob/master/SimplyAdvancedHelperLibrary/src/net/simplyadvanced/utils/OrientationUtils.java