問題描述:
由于業務需要設定了一個透明背景的Activity,但發現在Android8.0系統會崩潰
先介紹下設定方法
styles.xml中新增
<style name="TransparentTheme" parent="AppTheme">
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
</style>
給對應的Activity設定主題
<activity
android:name=".rn.TNTransparentReactNativeActivity"
android:configChanges="keyboardHidden|orientation|screenSize|screenLayout"
android:theme="@style/TransparentTheme"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden" />
然後報錯如下
Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
看來是不讓設定方向,沒辦法隻能去掉android:screenOrientation="portrait",但我又想固定方向怎麼辦呢
解決方案:
依舊去掉android:screenOrientation="portrait",然後在代碼中設定
在對應Activity的onCreate方法中設定
override fun onCreate(savedInstanceState: Bundle?) {
//android O fix bug orientation
if (android.os.Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
super.onCreate(savedInstanceState)
}
大功告成!繼續寫代碼~