在Android中运行时更改主题颜色 [英] Change Theme Color at Runtime in Android

查看:229
本文介绍了在Android中运行时更改主题颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在应用程序中添加了多个主题,并且可以在运行时轻松切换,则可以更改主题颜色。其他方法是逐个改变actiobar,textview等的颜色。这种方式很长,需要很多代码。我找到的简单方法是在colors.xml文件中更改颜色声明但我找不到编程方式或第三方库来执行此操作。有没有办法做到这一点。

Theme colors can be changed if multiple themes added in app and can be switched at run time easily. Other way is to change the color of actiobar, textview etc one by one of all the object. This way is very long and take lots of code. Simple way i found is to change colors declare in colors.xml file but i could not find a way programmatically or third party library to do that. Is there a way to do this.

我的颜色XML文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#ea1e63</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

我只想在运行时更改 colorPrimary 颜色,这样会改变颜色在所有应用程序活动。我找到了 https://github.com/negusoft/GreenMatter ,以便在运行时改变颜色,但是旧的和不适用于Android工作室。

I just want to change colorPrimary color at runtime, this will change color on all app activities. I found https://github.com/negusoft/GreenMatter, to change colors at runtime but its old and dont work with android studio.

推荐答案

当你应用主题并希望改变颜色而不是

when you apply theme and want to change color than

EX:

activity.setTheme(R.style.green);


<style name="green">
        <item name="main_background">@drawable/background_green</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="button_light">@color/button_light</item>
</style>

主题颜色不能在特定主题的运行时更改

如果要在应用程序中更改颜色,请设置自定义样式并更改这些样式。

Make custom style and change these styles when you want to change color in your application.

这篇关于在Android中运行时更改主题颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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