我使用两种样式制作了一个启动屏幕,第一种是默认样式,没有“windowBackground”,第二种是带有“窗口背景”的splash屏幕样式。问题是,当我使用getApplication().setTheme在MainActivity中使用下面的代码更改样式时:
@Override
protected final void onCreate(@Nullable Bundle savedInstanceState) {
// this line of code is not changing theme
getApplication().setTheme(R.style.Theme_Defaults);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
启动屏幕不会消失(“windowBackground”不会从“SpScreen”样式中删除)
宣言:
<application
android:hardwareAccelerated="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/SpScreen"
android:usesCleartextTraffic="true"
android:largeHeap="true"
android:extractNativeLibs="false"
tools:ignore="UnusedAttribute">
Theme.xml:
<!-- Base application theme. -->
<style name="Theme.Defaults" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:statusBarColor" tools:targetApi="lollipop">@color/white</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
<item name="colorPrimary">@color/colorInt</item>
<item name="colorAccent">@color/colorInt</item>
<item name="android:supportsPerformanceGameMode" tools:ignore="NewApi">true</item>
<item name="android:forceHasOverlappingRendering" tools:ignore="NewApi">true</item>
<item name="android:windowEnableSplitTouch">false</item>
<item name="android:splitMotionEvents">false</item>
<item name="android:allowGameFpsOverride" tools:ignore="NewApi">true</item>
<item name="android:largeHeap">true</item>
<item name="android:allowGameAngleDriver" tools:ignore="NewApi">true</item>
</style>
<style name="SpScreen" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:statusBarColor" tools:targetApi="lollipop">@color/white</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
<item name="colorPrimary">@color/colorInt</item>
<item name="colorAccent">@color/colorInt</item>
<item name="android:supportsPerformanceGameMode" tools:ignore="NewApi">true</item>
<item name="android:forceHasOverlappingRendering" tools:ignore="NewApi">true</item>
<item name="android:windowEnableSplitTouch">false</item>
<item name="android:splitMotionEvents">false</item>
<item name="android:allowGameFpsOverride" tools:ignore="NewApi">true</item>
<item name="android:largeHeap">true</item>
<item name="android:allowGameAngleDriver" tools:ignore="NewApi">true</item>
<item name="android:windowBackground">@drawable/backgroud_splashscreen</item>
</style>
两种风格的唯一区别是:
<item name="android:windowBackground">@drawable/backgroud_splashscreen</item>
发布于 2022-11-10 02:12:38
而不是:
getApplication().setTheme(R.style.Theme_Defaults);
试着使用:
getLayoutInflater().getContext().setTheme(R.style.Theme_Defaults);
这应该能行。
https://stackoverflow.com/questions/74370708
复制相似问题