Android 从片段内更改活动主题
我将首先介绍我的代码。看看主题:Android 从片段内更改活动主题,android,android-fragments,kotlin,themes,android-databinding,Android,Android Fragments,Kotlin,Themes,Android Databinding,我将首先介绍我的代码。看看主题: <resources> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimar
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:textAllCaps">false</item>
<item name="android:windowEnableSplitTouch">false</item>
</style>
<style name="AppTheme.Launcher">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowBackground">@drawable/launch_screen</item>
</style>
</resources>
在我的一个片段中,我想再次显示动作栏,我不想我的状态栏再是半透明的,或者换句话说,我希望它能被看到,并且有一个颜色PrimaryDark
。为此,我在main活动中编写了一个函数
fun clearTranslucent() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val w: Window = window
w.clearFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
)
w.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
}
}
这就是乐趣的开始。出于某种原因,在这两者之间添加了空白。如果我决定注释掉这一行
WindowManager.LayoutParams.FLAG\u transparent\u STATUS
,结果会更奇怪。状态栏变灰,我的滚动视图
停止工作。当我想要更改活动的原始主题时,同样的错误会在几个片段中出现。我试着在片段中改变主题,但没有成功。在某个地方读到,您只能从onCreate
函数中更改活动主题。为什么我的布局出现问题?您描述的问题是已知的错误
只能在此活动的onCreate发生之前设置活动的主题
所以你有两个选择:
所以,没有其他更简洁的方法可以做到这一点?这意味着每当我想从片段中隐藏或显示状态栏时,我都会调用.recreate()?很遗憾,是的
fun clearTranslucent() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val w: Window = window
w.clearFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
)
w.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
}
}