【问题标题】:Activity Theme is not changing活动主题不变
【发布时间】:2019-09-16 01:47:04
【问题描述】:

我的 Splash 活动不会改变,只有当我将名称更改为另一个时才会改变。

例如: "SplashActivity" - 从修改开始

如果我将主题更改为另一种颜色,我需要将活动重命名为: “SplashActivityy”或其他名称。

如果我回到名称“SplashActivity”,修改会回到后面。

已经格式化并清除了android studio的缓存,这没有帮助!

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.cobaiascreen">

    <application
        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/AppTheme"
        android:name=".App"
        tools:ignore="GoogleAppIndexingWarning">
        <activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAINACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

SplashActivity.java

package com.example.cobaiascreen;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Hiding Title bar of this activity screen */
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        //Making this activity, full screen */
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

App.java

package com.example.cobaiascreen;

import android.app.Application;
import android.os.SystemClock;

import java.util.concurrent.TimeUnit;

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        // Don't do this! This is just so cold launches take some time
        SystemClock.sleep(TimeUnit.SECONDS.toMillis(3));
    }
}

Styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/background_splash</item>
    </style>

</resources>

background_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@color/colorPrimaryDark"/>

    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/icon"/>
    </item>

</layer-list>

编辑:和我一样的问题: Splash Theme is not changing anymore

编辑 2: 我在一个线程中问了一个问题,以了解我的代码中发生了什么,经过一段时间的坐立不安后,我没有在互联网上找到与 walks 相关的内容。

已经尝试清除缓存,重新创​​建应用程序,清除文件缓存,已经在 android studio 中完成了所有操作。

过了一会儿,我发现有话要重启应用程序,但似乎会发生变化。

但我不希望用户安装我的应用程序时必须重新启动应用程序才能显示主题中的更改。我该如何解决这个问题?

【问题讨论】:

  • 尝试从 AndroidStudio 中禁用 Instant Run
  • 我已停用,但只有重启手机才能生效
  • 您使用的是哪款手机或模拟器?

标签: java android android-studio android-styles


【解决方案1】:

当我深入分析代码时,我发现代码中没有“你在说什么”的理由。所以我认为可能的原因是:

也许那个您有多个可绘制文件夹,并且文件 background_splash.xml 存在于您设备特定的可绘制文件夹中,可能是 drawable-xxhdpi还有drawable

& 很可能您正在对drawable 进行与您的设备无关的更改。

所以请验证它,样式也“如果适用”也是如此。

您可以采取的其他措施是

  • 禁用即时运行。
  • delete dataclear cache来自应用信息>手机的存储选项,然后重新安装。

希望以上任何一种技术都对您有用。

【讨论】:

    【解决方案2】:

    试试这个:

    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="android:windowBackground">@drawable/background_splash</item>
    </style>
    

    您确定android.intent.action.MAINACTIVITY 存在吗?

    【讨论】:

    • 我不认为这是代码。我需要更改活动的名称才能在移动设备上修改,我做了一个测试。我擦除了应用数据,它显示了修改,我不知道如何在 Android Studio 中修复它
    • 你试过我的解决方案了吗
    • 如果我将“background_splash”中的颜色更改为另一种颜色“”我需要重命名文件“SplashActivity”以显示更改我的手机。
    • Yash 我的问题是一样的stackoverflow.com/questions/40998841/…
    • 你删除了 android.intent.action.MAINACTIVITY
    【解决方案3】:

    android studio 的增量构建系统“Instant Run”存在问题。这就是为什么他们引入了新的/改进的增量构建,称为“应用更改”作为 Marble 项目的结果。它在 android studio 3.5 版中可用。请参阅文档here

    【讨论】:

    • 我已停用,但只有重新启动手机才能生效
    • 你可以试试模拟器吗?只是为了交叉验证这是设备问题还是工作室问题。
    • 我觉得是android 6.0,好像缓存了主题。是否有任何禁用此功能的功能?
    【解决方案4】:

    尝试将android:allowBackup="true" 更改为android:allowBackup="false"。安装应用程序。然后完全卸载它。重新安装。看主题。然后更改主题并重新安装 - 检查主题是否已更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 2013-01-20
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多