使用非不透明活动定位Android API 27时锁定屏幕方向

van*_*shg 35 java android android-activity

我有一个已android:windowIsTranslucent设置为trueandroid:windowBackground设置为半透明背景的活动.我刚刚更改了目标并将sdk版本编译为27,现在启动此活动时出现异常:

java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation

由于这是一个新的sdk,目前还没有关于它的任何内容(它似乎来自这行代码:https://android.googlesource.com/platform/frameworks/base.git/+/master/ core/java/android/app/Activity.java#987)

有没有办法解决这个问题?如果我android:screenOrientation="portrait"从我的清单中取出这项活动,该应用程序不会崩溃,但我希望能够保持这样.

小智 33

我也遇到了同样的问题.正如其他人所说,如果我删除android:screenOrientation ="portrait"或用android:screenOrientation ="unspecified"覆盖它,那么异常就消失了.并且前方活动的方向似乎遵循背后活动的方向.

我想过这个问题.如果前方活动是透明的并且后方活动的方向不同,则显示变得奇怪.所以,我可以理解为什么添加了这个检查逻辑但是,我想知道为什么在Developer Preview 8.0.0中没有出现这个问题.


Jer*_*kub 17

解决方法是设置targetSdk26.

之所以是你的应用程序崩溃是这里在这个承诺.

正如您在此处所看到的,您并不是唯一的一个 - 这种行为已被Google报告为问题.它已被修复,但我们不知道它将如何以及何时发布.


我还可以在评论中确认"sofakingforever"是什么,如果在半透明后面有固定方向的非半透明活动,半透明将不会旋转.所以你也可以android:screenOrientation="portrait"从清单中删除.

  • 这不是一个真正的解决方案.如果有人针对API 27,那是有原因的. (4认同)
  • @HarishVishwakarma是的,你是绝对正确的.所以我将"解决方案"改为"解决方法".并不是永远,因为谷歌将强制将新的APK上传到GPlay以定位最新的API. (2认同)

ome*_*sem 6

对我有用的解决方案是删除

android:screenOrientation="portrait" 
Run Code Online (Sandbox Code Playgroud)

来自所有全屏透明活动,这意味着它们的主题包含

<item name="android:windowIsTranslucent">true</item>
Run Code Online (Sandbox Code Playgroud)

另外,为了确保定向在Oreo以下均正确,我将其添加到活动的onCreate()中。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // This activity is a fullscreen transparent activity, so after Oreo Android doesn't allow fullscreen
    // transparent activities to specify android:screenOrientation="portrait" in the manifest. It will pick up
    // from the background activity. But for below Oreo we should make sure that requested orientation is portrait.
    if (VERSION.SDK_INT < VERSION_CODES.O) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}
Run Code Online (Sandbox Code Playgroud)