Activity orientation changes automatically on Android

前端 未结 7 2065
天命终不由人
天命终不由人 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:53

    I also faced same problem for Android N devices. So to support both orientations for tablets and only portrait for phones I did the following trick:

    1. Set portrait to each activity in Manifest. Looks bad, but in this case screen will not autorotate: android:screenOrientation="portrait"

    2. In your BaseActivity set:

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if (isTablet(){  
             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
          }
      }
      
      protected boolean isTablet() {
         return getResources().getBoolean(R.bool.tablet);
      }
      
    0 讨论(0)
提交回复
热议问题