使用values\themes.xml & values\attrs.xml + values\colors.xml通过下面的成功(未完全测试)更改背景
*Ensure any themes created in values\styles.xml are not duplicated in themes.xml (i.e same key name <style name="LightLoginThemeV3">)*
在下面添加到values\colors.xml (应该已经存在......)
<!-- light Theme colors -->
<color name="lightBackgroundPrimaryV3">@color/white</color>
<color name="lightBackgroundSecondaryV3">@color/lighter_grey</color>
<color name="lightBackgroundAltV3">@color/lighter_grey</color>
<color name="lightBackgroundAltAlphaV3">#809E9E9E</color>
<color name="lightBackgroundLightV3">#80FFFFFF</color>
<color name="lightThemePrimaryColourV3">#01aac4</color>
<color name="lightThemeSecondaryColourV3">#025f8b</color>
<color name="lightThemeDarkPrimaryColourV3">@color/white</color>
<color name="lightThemeDarkSecondaryColourV3">@color/lighter_grey</color>
<!-- dark Theme colors -->
<color name="darkBackgroundPrimaryV3">#241e45</color>
<color name="darkBackgroundSecondaryV3">#2f2856</color>
<color name="darkBackgroundAltV3">#483e81</color>
<color name="darkBackgroundAltAlphaV3">#80483e81</color>
<color name="darkBackgroundLightV3">#f4f3f3</color>
<color name="darkThemePrimaryColourV3">#01aac4</color>
<color name="darkThemeSecondaryColourV3">#025f8b</color>
<color name="darkThemeDarkPrimaryColourV3">#2f2856</color>
<color name="darkThemeDarkSecondaryColourV3">#50448f</color>
values\attrs.xml (如果不存在,则在 values 文件夹中创建 attrs.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- background color keys -->
<attr name="basePrimaryBackgroundColour" format="reference|color"/>
<attr name="baseSecondaryBackgroundColour" format="reference|color"/>
</resources>
values\themes.xml (如果不存在,则在 values 文件夹中创建 attrs.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="LightLoginThemeV3">
<!-- keys from values/attrs.xml file -->
<!-- background color keys -->
<!-- using light Theme colors via @color -->
<item name="basePrimaryBackgroundColour">@color/lightBackgroundPrimaryV3</item>
<item name="baseSecondaryBackgroundColour">@color/lightBackgroundSecondaryV3</item>
<item name="windowNoTitle">true</item>
<!-- Support library compatibility -->
<item name="windowActionBar">false</item>
<item name="android:windowBackground">@color/backgroundSecondaryV3</item>
<item name="android:popupBackground">@color/backgroundSecondaryV3</item>
<!-- EditText Fields -->
<item name="colorControlNormal">@color/medium_grey</item>
<item name="colorControlActivated">@color/themePrimaryColourV3</item>
<item name="colorControlHighlight">@color/themePrimaryColourV3</item>
</style>
<style name="DarkLoginThemeV3">
<!-- keys from values/attrs.xml file -->
<!-- background color keys -->
<!-- using dark Theme colors via @color -->
<item name="basePrimaryBackgroundColour">@color/darkBackgroundPrimaryV3</item>
<item name="baseSecondaryBackgroundColour">@color/darkBackgroundSecondaryV3</item>
<item name="android:windowNoTitle">true</item>
<!-- Support library compatibility -->
<item name="windowActionBar">false</item>
<item name="android:windowBackground">@color/colorTrans</item>
<item name="android:popupBackground">@color/darkist_grey</item>
<!-- EditText Fields -->
<item name="colorControlNormal">@color/dark_grey</item>
<item name="colorControlActivated">@color/colorPrimary</item>
<item name="colorControlHighlight">@color/colorPrimary</item>
</style>
</resources>
并在我的相关布局文件中,像这样设置背景......
//key from values/attrs.xml file
android:background="?attr/basePrimaryBackgroundColour"
并在我的活动 onCreate() 方法(和 onResume() 方法)中,通过下面的动态从 values\themes.xml 设置主题...
if(darkTheme){
setTheme(R.style.DarkLoginThemeV3);
}else{
setTheme(R.style.LightLoginThemeV3);
}
在我的活动中,通过 clickListner 事件,通过下面的动态从 values\themes.xml 更改主题...
if(darkTheme){
setTheme(R.style.DarkLoginThemeV3);
recreate();
}else{
setTheme(R.style.LightLoginThemeV3);
recreate();
}