除了在活动的 onCreate 方法中检查设备类型之外,是否有通用的方法来设置屏幕方向
Is there a generic way to set the Screen Orientation other than checking for device type in onCreate method of your Activities
这是我尝试过的方法,并且有效。
在 res/values 作为 bool.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">true</bool>
</resources>
在 res/values-sw600dp 和 res/values-xlarge 中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">false</bool>
</resources>
在我的活动的 onCreate 方法中:
if(getResources().getBoolean(R.bool.portrait_only)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
除了检查每个 oncreate 方法之外,还有其他方法可以实现完全相同的结果
要么你为activity这样的
创建一个基础class
//extending this to all of your activities isnt really that expensive
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getResources().getBoolean(R.bool.portrait_only)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
...
OR
在您的 Application
class 中注册生命周期回调
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
if(getResources().getBoolean(R.bool.portrait_only)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Log.i(TAG , "ApplicationClass-OnActivityCreated()");
}
@Override
public void onActivityStarted(Activity activity) {
Log.i(TAG , "ApplicationClass-OnActivityStarted()");
}
@Override
public void onActivityResumed(Activity activity) {
Log.i(TAG , "ApplicationClass-OnActivityResumed()");
}
@Override
public void onActivityPaused(Activity activity) {
Log.i(TAG , "ApplicationClass-OnActivityPaused()");
}
@Override
public void onActivityStopped(Activity activity) {
Log.i(TAG , "ApplicationClass-OnActivityStopped()");
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
Log.i(TAG , "ApplicationClass-OnActivitySaveInstanceState()");
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.i(TAG , "ApplicationClass-OnActivityDestroyed()");
}
});
}
}
但我仍然更喜欢基础 class 实现。
希望对您有所帮助:)
这是我尝试过的方法,并且有效。 在 res/values 作为 bool.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">true</bool>
</resources>
在 res/values-sw600dp 和 res/values-xlarge 中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">false</bool>
</resources>
在我的活动的 onCreate 方法中:
if(getResources().getBoolean(R.bool.portrait_only)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
除了检查每个 oncreate 方法之外,还有其他方法可以实现完全相同的结果
要么你为activity这样的
创建一个基础class//extending this to all of your activities isnt really that expensive
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getResources().getBoolean(R.bool.portrait_only)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
...
OR
在您的 Application
class 中注册生命周期回调
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
if(getResources().getBoolean(R.bool.portrait_only)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Log.i(TAG , "ApplicationClass-OnActivityCreated()");
}
@Override
public void onActivityStarted(Activity activity) {
Log.i(TAG , "ApplicationClass-OnActivityStarted()");
}
@Override
public void onActivityResumed(Activity activity) {
Log.i(TAG , "ApplicationClass-OnActivityResumed()");
}
@Override
public void onActivityPaused(Activity activity) {
Log.i(TAG , "ApplicationClass-OnActivityPaused()");
}
@Override
public void onActivityStopped(Activity activity) {
Log.i(TAG , "ApplicationClass-OnActivityStopped()");
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
Log.i(TAG , "ApplicationClass-OnActivitySaveInstanceState()");
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.i(TAG , "ApplicationClass-OnActivityDestroyed()");
}
});
}
}
但我仍然更喜欢基础 class 实现。
希望对您有所帮助:)