问题描述
我要创造我的preference活动复选框,允许用户切换方向改变。
在类似问题的人写的只有完整的方向锁定(通过覆盖onConfigurationChanged方法或在AndroidManifest.xml中加入configChanges)或方向执行(由setRequestedOrientation)。
有没有一种方法来切换方向锁定?
编辑:我创建了集preferred取向三种状态的方法:横向,纵向和传感器。这种方法结合使用检索方向 getResources()。getConfiguration()。方向)
和保存检索定位到preferences。然后在需要锁定方位的活动我火了这种方法从preferences preferred方向。
私有静态无效setActivityOrientation(活动活动,诠释preferenceOrientation){
如果(preferenceOrientation == Configuration.ORIENTATION_LANDSCAPE){
如果(activity.getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//你需要检查,如果你想要的方向尚未设置,因为设置的方向重新启动您的活动这需要很长
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}否则,如果(preferenceOrientation == Configuration.ORIENTATION_PORTRAIT){
如果(activity.getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
} 其他 {
如果(activity.getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_SENSOR){
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}
}
我不明白什么是与setRequestedOrientation的问题。
在SCREEN_ORIENTATION_SENSOR相结合,横向或纵向似乎你想要的,不是吗?
如果(....)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
否则,如果(....)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
否则,如果(....)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
I want to create checkbox in my preference Activity that allows user to toggle orientation change.
In similar questions people write only about complete orientation lock (by overriding onConfigurationChanged method or adding configChanges in AndroidManifest.xml) or orientation enforcing ( by setRequestedOrientation ).
Is there a way to toggle orientation lock?
EDIT:I've created a method that sets preferred orientation to one of three states: landscape, portrait and sensor. This method is used in conjunction with retrieving orientation getResources().getConfiguration().orientation)
and saving retrieved orientation into preferences. Then in activity that needs to lock orientation I fire this method with preferred orientation from preferences.
private static void setActivityOrientation(Activity activity, int preferenceOrientation) {
if (preferenceOrientation == Configuration.ORIENTATION_LANDSCAPE) {
if( activity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
// You need to check if your desired orientation isn't already set because setting orientation restarts your Activity which takes long
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
} else if (preferenceOrientation == Configuration.ORIENTATION_PORTRAIT) {
if( activity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
} else {
if( activity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_SENSOR){
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}
}
I don't understand what is the problem with the setRequestedOrientation.
The SCREEN_ORIENTATION_SENSOR combine to landscape or portrait seem what you want, no?
if(....)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
else if(....)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else if(....)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
这篇关于如何在Android的切换方向锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!