gpt4 book ai didi

java - 更改在 colors.xml 中设置的 styles.xml 中的颜色

转载 作者:行者123 更新时间:2023-11-29 23:17:24 24 4
gpt4 key购买 nike

我在我的 colors.xml 中设置了自定义颜色,如下所示,但是,是否可以按照以下更改/更新我的 styles.xml 主题节点中的颜色;

colors.xml

<resources>
<color name="themeBackground">#000000</color>
<color name="darkColor">#000000</color>
<color name="lightColor">#ffffff</color>
<resources>

并在我的 v21\styles.xml 中按照以下内容手动更改

//used in activity
onCreate().setTheme(R.style.DarkTheme);

v21\styles.xml

<style name="DarkTheme">
<!-- change themeBackground manually here -->
<item name="themeBackground">@color/darkColor</item>
</style>

<style name="LightTheme">
<!-- change themeBackground manually here -->
<item name="themeBackground">@color/lightColor</item>
</style>

我已经试过了,但运气不好,因为似乎只能更改 android 值,即

<item name="colorPrimary">@color/lightColor</item>

最佳答案

使用 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"

在我的 Activity onCreate() 方法(和 onResume() 方法)中,通过下面的 values\themes.xml 动态设置主题 ...

if(darkTheme){
setTheme(R.style.DarkLoginThemeV3);
}else{
setTheme(R.style.LightLoginThemeV3);
}

在我的 Activity 中,通过 clickListner 事件,通过下面的 values\themes.xml 动态更改主题 ...

if(darkTheme){
setTheme(R.style.DarkLoginThemeV3);
recreate();
}else{
setTheme(R.style.LightLoginThemeV3);
recreate();
}

关于java - 更改在 colors.xml 中设置的 styles.xml 中的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55085154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com