以编程方式更改XML颜色 [英] Change XML color programmatically

查看:61
本文介绍了以编程方式更改XML颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过Kotlin/java代码更改XML颜色.我的应用程序中只有5种颜色,在设置中您可以更改主题.我不想将每个元素更改为新颜色,但是我想更改XML颜色,以便所有元素都获得新颜色.例如:

Is it possible to change XML colors via Kotlin/java code. There's only 5 colors in my app, and in the settings you can change the theme. I don't want to change each element to the new color, but I want to change the XML color so all the elements get the new colors. For example:

<color name="colorPrimary">#6c6c6c</color>

每当开关元素 ON

<color name="colorPrimary">#212121</color>

主要思想是我希望所有元素都从源头获得颜色.

The main Idea is I want all the elements to get their colors from a source.

推荐答案

无法在运行时更改@CommonsWare注释的XML资源值.

It is not possible to change XML resource values at runtime as @CommonsWare commented.

为了动态更改所有颜色,最好的方法是为此使用应用程序主题.创建一个新主题并以编程方式对其进行更改.

In order to change all colors dinamically, the best way is to use app themes for that. Create a new theme and change it programmatically.

请参阅:主题

这篇关于以编程方式更改XML颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆