1

我已经在我的 android 应用程序中实现了深色 ui,一切正常,但我有一个启动器活动,它windowBackground的风格像这样

   <style name="AppTheme.Launcher" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="android:windowBackground">@drawable/l_launch_screen</item>
    </style>

l_launch_screen

<?xml version="1.0" encoding="utf-8"?>

<!-- The android:opacity=”opaque” line — this is critical in preventing a flash of black as your theme transitions. -->
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">

    <!-- The background color, preferably the same as your normal theme -->
    <item android:drawable="@color/colorPrimaryDark"/>

    <!-- Your product logo - 144dp color version of your app icon -->
    <item>
        <bitmap
            android:src="@drawable/app_round_icon"
            android:gravity="center"/>
    </item>

</layer-list>

并且这colorPrimaryDark有两种颜色,一种是夜晚,另一种很简单现在问题是,当通过在 android q 通知面板中选择来激活深色主题时Dark theme,android q 通知面板android:windowBackground正在选择夜间颜色,但是当Dark theme被 android 禁用并且在我的中选择了深色主题时应用程序通过在应用程序类中设置它

   AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

android:windowBackground没有选择夜间颜色,但所有其他活动都在完美地选择夜间颜色我已经看到与WhatsApp应用程序相同的行为所以这是一个错误还是我做错了什么我也尝试为夜间和可绘制定义不同的样式但正在发生同样的问题

4

1 回答 1

0

这是如何使用活动方法实现加载屏幕。

  1. 添加2个新资源文件:
  • 对于浅色主题splash_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/white" />
    <item>
        <bitmap android:gravity="center"
            android:src="@drawable/logo"/>
    </item>
</layer-list>
  • 对于深色主题splash_background.xml(同名)将其放在drawable-night文件夹中(如果没有则创建),并且您必须添加colors.xml自定义深色(例如<color name="black_700">#191414</color>):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/your_dark_background_color" />
    <item>
        <bitmap android:gravity="center"
            android:src="@drawable/logo"/>
    </item>
</layer-list>
  1. 最后在你的themes.xml中:
    <style name="Theme.AppName.SplashTheme" parent="Theme.AppName">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>
  1. 将启动画面创建为现有样式的主题并在应用程序启动时显示它的好方法:为此创建新的 Activity
class SplashScreenActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        setTheme(R.style.Theme_AppName_SplashTheme)
        super.onCreate(savedInstanceState)

        startActivity(Intent(this,MainActivity::class.java))
        overridePendingTransition(0, 0) //this one to remove animation
        finish()
    }
}
  1. 将此主题应用到您的 SplashScreenActivity 中AndroidManifest.xml
        <activity
            android:name=".SplashScreenActivity"
            android:excludeFromRecents="true"
            android:exported="true"
            android:launchMode="singleInstance"
            android:theme="@style/Theme.AppName.SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.MULTIWINDOW_LAUNCHER" />
            </intent-filter>
        </activity> 
  1. 在您的 MainActivity.kt 中,您必须添加 Intent.FLAG_ACTIVITY_NO_ANIMATION 以在切换活动时消除烦人的滑动动画:
    companion object {
        fun startFrom(context: Context) {
            val intent = Intent(context, MainActivity::class.java)
            intent.flags =
                Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NO_ANIMATION 
            context.startActivity(intent)
        }
    }
于 2021-09-09T13:12:25.657 回答