Activity orientation changes automatically on Android

前端 未结 7 2070
天命终不由人
天命终不由人 2020-12-10 01:50

I\'m developing a mobile application based on Android with minSdkVersion=15. I would like to support both orientations for tablets and only portrait for smartph

7条回答
  •  爱一瞬间的悲伤
    2020-12-10 02:34

    Here's a good way using resources and size qualifiers.

    Put this bool resource in res/values as bools.xml or whatever (file names don't matter here):

    
    
        true
    
    

    Put this one in res/values-sw600dp and res/values-xlarge:

    
    
        false
    
    

    See this supplemental answer for help adding these directories and files in Android Studio.

    Then, in the onCreate method of your Activities you can do this:

    if(getResources().getBoolean(R.bool.portrait_only)){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    

    Devices that are more than 600 dp in the smallest width direction, or x-large on pre-Android 3.2 devices (tablets, basically) will behave like normal, based on sensor and user-locked rotation, etc. Everything else (phones, pretty much) will be portrait only.

    Source: Original answer

提交回复
热议问题