以编程方式更改整个应用程序的文本颜色 [英] Change whole application's text color programmatically

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

问题描述

我想改变整个应用程序的文本背景 颜色在Java中,这可能吗?有了这个,我的意思是要改变的每次项的颜色在应用程序中( TextViews,ListView控件 的项目,一切)。

I would like to change the whole application's text and background color in Java, is this possible? With this I mean to change the color of every item in the application (TextViews, ListView items, everything).

这可能吗?

我用一个特制的风格都尝试过,但我不能使它发挥作用。下面是XML文件(放在 RES /布局/值文件夹):

I have tried using a custom-made style but I can't make it work. Here is the xml file (put in the res/layout/values folder):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Light">
        <item name="android:textColor">#00FF00</item>
    </style>
</resources>

比方说,我只是想改变的文本颜色了。

Let's say I just want to change the text color for now.

现在我把这种风格在我的应用程序是这样的:

Now I call this style in my application like this:

public void onCreate(Bundle icicle) {

    Activity.setTheme(android.R.style.light);
    super.onCreate(icicle);
    setContentView(R.layout.main);

但我得到的错误光不能得到解决,或者不是一个领域

我发现以编程方式做到这一点的一种方法是重启活动,号召

One way I found to do this programmatically is to restart the activity, calling

this.setTheme(R.style.Light);
onCreate(null);

不过,这仅适用于当前的活动,而不是整个应用程序。这将是巨大的,如果它是有可能做到这一点发起的另一个活动,不仅是当前的。

However, this works only for the current activity and not for the whole application. It would be great if it were possible to do this launching another activity, not only the current one.

推荐答案

好了,所以我发现了一个可能的解决方案,这是通过使用意图和活动的 putExtra 方法。

Ok so I found one possible solution, which is to pass the theme information between the activities using intents and the putExtra method.

$ C $下的第一个活动(来电):

Code for the first activity (the caller):

Intent i = new Intent(this, ActivityToCall.class);
i.putExtra("key", R.style.Light);
startActivity(i);

code的第二个活动(被调用的):

Code for the second activity (the called one):

public void onCreate(Bundle icicle) {
    int theme = getIntent().getIntExtra("key",-1);
    this.setTheme(theme);
    super.onCreate(icicle);
    // other code...

我不知道这是否是最好的方法,但至少它的工作原理。

I don't know if it's the best possible approach but at least it works.

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

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