сначала создайте 2 стиля с другим именем в стиле. xml
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">#fff</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="PrimaryTextColor">#fff</item>
</style>
<style name="AppThemeDark" parent="Theme.MaterialComponents.NoActionBar">
<item name="colorPrimary">#2196F3</item>
<item name="colorPrimaryDark">#303030</item>
<item name="colorAccent">#03A9F4</item>
<item name="PrimaryTextColor">#03A9F4</item>
</style>
и определите эти стили
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
<style name="AppThemeDark.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
<style name="Light" parent="AppTheme.NoActionBar"/>
<style name="Dark" parent="AppThemeDark.NoActionBar"/>
создайте файл attrs. xml в папке значений, которую вы Вы должны определить свой цветовой ключ здесь
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="PrimaryTextColor" format="color"/>
<attr name="SecondaryTextColor" format="color"/>
</resources>
, теперь в макете. xml Вы должны установить цвет следующим образом:
android:background="?attr/PrimaryTextColor"
и, наконец, для изменения темы для Dark
setTheme(R.style.Dark)
recreate()
для Light
setTheme(R.style.Light)
recreate()
ОБНОВЛЕНИЕ: поместите эти стили в значения-v19. xml и значения-v21. xml стиль. xml файл
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">false</item>
</style>
<style name="AppThemeDark.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">false</item>
</style>
и ваша тема в AndroidManifest. xml должна быть:
android:theme="@style/AppTheme.NoActionBar"