Android Studio Android主题不变

flvlnr44  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(190)

我使用两种样式制作了一个闪屏,第一种是不带“windowBackground”的默认样式,第二种是带“windowBackground”的闪屏样式。问题是,当我使用下面的代码在MainActivity中使用getApplication().setTheme更改样式时:

@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);

初始屏幕未消失(未从“SpScreen”样式中删除“windowBackground”)

清单:

<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">

主题.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>
62o28rlo

62o28rlo1#

而不是:

getApplication().setTheme(R.style.Theme_Defaults);

尝试使用:

getLayoutInflater().getContext().setTheme(R.style.Theme_Defaults);

这应该可以

相关问题