java 锁定具有非不透明活动的 Android API 27 时的屏幕方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46980697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lock screen orientation when targeting Android API 27 with a non-opaque activity
提问by vanshg
I have an activity that has android:windowIsTranslucent
set to true
and android:windowBackground
set to a translucent background. I just changed my target and compile sdk version to 27, and I get an exception when launching this activity now:
我有一个具有一个活动android:windowIsTranslucent
设置true
,并android:windowBackground
设置成半透明的背景。我刚刚更改了目标并将 sdk 版本编译为 27,现在启动此活动时出现异常:
java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
Since this is a new sdk, there isn't anything online about it yet (and it seems to result from this line of code: https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/android/app/Activity.java#987)
由于这是一个新的 sdk,所以网上还没有关于它的任何信息(似乎是由这行代码产生的:https: //android.googlesource.com/platform/frameworks/base.git/+/master/核心/java/android/app/Activity.java#987)
Is there any way to get around this? The app doesn't crash if I take out android:screenOrientation="portrait"
from my manifest for this activity, but I would like to be able to keep it like that.
有什么办法可以解决这个问题吗?如果我android:screenOrientation="portrait"
从我的清单中取出此活动,应用程序不会崩溃,但我希望能够保持这种状态。
采纳答案by Takao Sumitomo
I also faced the same problem. As others said, If I deleted android:screenOrientation="portrait"or overrided it with android:screenOrientation="unspecified", then the exception was gone. And it seems that the front activity's orientation follows the behind activity's orientation.
我也面临同样的问题。正如其他人所说,如果我删除android:screenOrientation="portrait"或用android:screenOrientation="unspecified"覆盖它,那么异常就消失了。并且似乎前面活动的方向跟随后面活动的方向。
I thought about it. If the front activity is transparent and the behind activity's orientation is different, the display becomes strange. So, I can understand why this check logic was added However, I wonder that why this problem was not occurred in Developer Preview 8.0.0.
我想过这个问题。如果前面的 Activity 是透明的,后面的 Activity 的方向不同,显示就会变得奇怪。所以,我可以理解为什么添加了这个检查逻辑,但我想知道为什么在Developer Preview 8.0.0中没有出现这个问题。
回答by JerabekJakub
The workaround is to set targetSdk
back to 26
.
解决方法是设置targetSdk
回26
.
The reason why is your application crashes is herein this commit.
之所以是你的应用程序崩溃是这里在这个承诺。
As you can see here, you are not the only one - this behavior has been reported to Google as issue. It has been fixed, but we don't know how and when it will be released.
正如您在此处看到的,您不是唯一一个 - 这种行为已作为问题报告给 Google。它已被修复,但我们不知道它将如何以及何时发布。
I can also confirm what "sofakingforever" says in comments, if there is non-translucent activity with fixed orientation behind your translucent, the translucent will not rotate. So you can just remove android:screenOrientation="portrait"
from manifest as well.
我还可以确认评论中“sofakingforever”所说的内容,如果在您的半透明后面有固定方向的非半透明活动,则半透明将不会旋转。所以你也可以android:screenOrientation="portrait"
从清单中删除。
回答by omersem
The solution worked for me is deleting
对我有用的解决方案是删除
android:screenOrientation="portrait"
from all the full screen transparent activities which means their theme contains
来自所有全屏透明活动,这意味着它们的主题包含
<item name="android:windowIsTranslucent">true</item>
Also to make sure that orientation works correct for below Oreo I added this to the onCreate() of the activities.
此外,为了确保 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);
}
}
回答by Venkatesh
I solved this issues by changing this line in NoActionBar styles
我通过在 NoActionBar 样式中更改这一行解决了这个问题
In target version 27 only i got this issue and i solved by using below line
在目标版本 27 中,只有我遇到了这个问题,我通过使用以下行解决了
<item name="android:windowIsTranslucent">false</item>
回答by Diogo Rosa
So what I did was remove any screenOrientation property from manifest and add it to my BaseActivity (from which all my activities extend), this code
所以我所做的是从清单中删除任何 screenOrientation 属性并将其添加到我的 BaseActivity(我的所有活动都从中扩展),这段代码
if(!(this instanceof TranslucentActivity)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
The TranslucentActivity will have the orientation from the Activity behind.
TranslucentActivity 将具有来自后面的 Activity 的方向。
回答by Brahem Mohamed
it seems like it's a new feature/bug on API 27. However, you can delete android:screenOrientation Or android:screenOrientation="unspecified"
它似乎是 API 27 上的一个新功能/错误。但是,您可以删除 android:screenOrientation 或 android:screenOrientation="unspecified"
回答by MD. Shafiul Alam Biplob
I recently faced the issue and here's the solution.
我最近遇到了这个问题,这是解决方案。
No need to change the screen orientation parameter which you set at the android manifest file.
无需更改您在 android manifest 文件中设置的屏幕方向参数。
Just add two folders in
只需在其中添加两个文件夹
res>values
as res>values-v26
and res>values-v27
Then copy your styles.xml and themes.xml file there.
然后在那里复制你的styles.xml 和themes.xml 文件。
and change the following parameters from TRUE to FALSE.
并将以下参数从 TRUE 更改为 FALSE。
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowIsTranslucent">false</item>
It will work.
它会起作用。
A common bug of Android 8.0
Android 8.0的一个常见bug
回答by flame3
Thanks @JerabekJakub. My test result - keep sdk 27 and remove the following lines can also solve the crash.
谢谢@JerabekJakub。我的测试结果-保留sdk 27并删除以下几行也可以解决崩溃问题。
android:configChanges="orientation"
android:screenOrientation="portrait"
回答by Attaullah
1) Remove this
1)删除这个
android:screenOrientation="portrait"
from minifiest.xml
来自 minifyingt.xml
2) on Activity add these two lines
2)在Activity上添加这两行
protected void onCreate(Bundle savedInstanceState) {
setOrientation(this)
super.onCreate(savedInstanceState);
// other other all code here
}
3) Just copy-paste the code in your Activity
3)只需将代码复制粘贴到您的活动中
public static void setOrientation(Activity context) {
if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.O)
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
else
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}