Android 8.0 Only fullscreen activities can request orientation解决方法
原文:Android 8.0 Only fullscreen activities can request orientation解决方法 | Stars-One的杂货小窝
公司的项目坑太多,现在适配高版本的出现了上面的这个问题..
个人项目中我一般不对这个Activity朝向orientation属性进行更改
出现这个问题的原因是Android8.0的限制,只有全屏(fullscreen)的Activity才能去设置横/竖屏的方向,所以解决方法从下面几个方向入手
分析
从版本入手,降低版本,低版本没有这个限制
从Activity方向入手,取消设置横/竖屏(也就是AndroidMainfest中删除Activity的orientation的属性),或者是将Activity设置为全屏的
那么怎么知道Activity是不是属于全屏的,由下面的属性可以判断(定义在style文件里)
1.“windowIsTranslucent”为true; 2.“windowIsTranslucent”为false,但“windowSwipeToDismiss”为true; 3.“windowIsFloating“为true;
符合上面的某点规则的,说明Activity不是fullScreen
我们按照上面的规则,在styles.xml文件中定义好主题,之后给Activity设置即可
上面说的,可能项目的Activity过多,不好更改,还可以使用代码的方式,将Activity的orientation属性还原为默认值
SCREEN_ORIENTATION_UNSPECIFIED(通过反射)
还有一点可以考虑,就是让非全屏的Activity的orientation属性设置
behind,让它跟随上个Activity保持相同的屏幕方向即可
基于以上的分析,总结得出下面几种解决方法
解决方法
修改targetSdk版本
将build.gradle里的targetSdk修改为26以下(包含26)
设置Activity为全屏
通过定义style.xml属性,将Activity设置为全屏即可
1.“windowIsTranslucent”为true; 2.“windowIsTranslucent”为false,但“windowSwipeToDismiss”为true; 3.“windowIsFloating“为true;
符合上面的某点规则的,说明Activity不是fullScreen,按照上面的定义,反向设置即可,如
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="windowIsTranslucent">false</item> <item name="windowSwipeToDismiss">false</item> </style>
然后在AndroidManiFest.xml文件中,给Activity设置style属性,如
<activity android:name="com.tjsoft.advice.ui.More" android:screenOrientation="landscape" android:theme="@style/AppTheme" />
通过反射重置orientation
具体参考Only fullscreen activities can request orientation终极解决方法_LiteHeaven的专栏-CSDN博客,本人未测试过
设置orientation为behind
这种方法是将非fullscreen的Activity的orientation设置为behind,让其屏幕方向和上个Activity相同,这样就不会出现问题了
删除Activity设置的orientation属性
这个是终极大法,就是不给Activity设置orientation,那么就不会出现问题啦
参考
- Android Api 27 在 Android 8.0 上出现 Only fullscreen opaque activities can request orientation 的解决情况...
- Android 8.0 崩溃,Only fullscreen activities can request orientation
- Android 8.0透明Activity报错 "Only fullscreen activities can request orientation"
- 日常工作记录:安卓8.0 Only fullscreen opaque activities can request orientation解决办法
- Android版本问题记录:Android 8.0下异常Only fullscreen opaque activities can request orientation
- 安卓开发——android8.0应用崩溃,报错: Only fullscreen opaque activities can request orientation
- java.lang.IllegalStateException: Only fullscreen activities can request orientation
- Android 8.0 Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can reques
- MTK 8.1遇到“Only fullscreen opaque activities can request orientation”报错处理
- Only fullscreen opaque activities can request orientation
- MTK 8.1遇到“Only fullscreen opaque activities can request orientation”报错处理
- Only fullscreen opaque activities can request orientation 原因及解决方案
- 安卓开发——android8.0应用崩溃,报错: Only fullscreen opaque activities can request orientation
- Only fullscreen activities can request orientation
- android8.0应用崩溃,报错: Only fullscreen opaque activities can request orientation
- Only fullscreen opaque activities can request orientation
- 报错:Only fullscreen opaque activities can request orientation
- android8.0报错:java.lang.IllegalStateException: Only fullscreen opaque activities can request orientat
- android开发中出现Only the original thread that created a view hierarchy can touch its views.错误的解决方法
- Android DialogFragment fullscreen 解决方法