首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android的主题没有改变

Android的主题没有改变
EN

Stack Overflow用户
提问于 2022-11-09 06:26:57
回答 1查看 29关注 0票数 0

我使用两种样式制作了一个启动屏幕,第一种是默认样式,没有“windowBackground”,第二种是带有“窗口背景”的splash屏幕样式。问题是,当我使用getApplication().setTheme在MainActivity中使用下面的代码更改样式时:

代码语言:javascript
运行
复制
@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”样式中删除)

宣言:

代码语言:javascript
运行
复制
 <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:

代码语言:javascript
运行
复制
<!-- 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>

两种风格的唯一区别是:

代码语言:javascript
运行
复制
<item name="android:windowBackground">@drawable/backgroud_splashscreen</item>
EN

回答 1

Stack Overflow用户

发布于 2022-11-10 02:12:38

而不是:

代码语言:javascript
运行
复制
getApplication().setTheme(R.style.Theme_Defaults);

试着使用:

代码语言:javascript
运行
复制
getLayoutInflater().getContext().setTheme(R.style.Theme_Defaults);

这应该能行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74370708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档