在非Activity类中锁定屏幕方向

时间:2013-10-14 09:54:12

标签: android android-layout

我希望非活动静态类中的锁定屏幕方向,如此

public class ScreenUtil {

private static Context context;

public static void Init(Context context) {
    ScreenUtil.context = context;
}

public static boolean lock() {
    if (((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) ||
    ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE)) {
        return false;
    } else {
        context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        return true;
    }
}

}

但是在锁定方法上Eclipse希望将上下文转换为Object

((Object) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

仍然想要改变演员

我的问题是,如果我不能改变这样的方向(在静态非活动类中)以及如何修复它

感谢所有回答

1 个答案:

答案 0 :(得分:0)

将活动参考作为参数传递给init方法

private static Activity context;

public static void Init(Activity context) {
    ScreenUtil.context = context;
}

然后你可以使用

context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);