Change whole application's text color programmatically

我与影子孤独终老i 提交于 2020-01-13 04:44:06

问题


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).

Is this possible?

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);

But I get the error light cannot be resolved or is not a field.

Update:

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.


回答1:


You're trying it in a bit to simple way. Like this you're just adjusting your general Activity's background instead of all the different Views that are out there.

In order to try and adjust every type of View (Button, TextView etc) you'll need to address all their own styles to overwrite them.

Per example if you want to adjust Button you'll need in your own general style:

 <item name="android:buttonStyle">@style/ButtonHoloDark</item>

This will point at your own custom style, which takes its parent from the Android's standard Button.

<style name="ButtonHoloDark" parent="android:style/Widget.Button">
    <item name="android:background">@drawable/btn_default_holo_dark</item>
    <item name="android:textColor">#ffffff</item>
</style>

Be warned, doing this for every View will take you quite some themes and styles.

You can find a great example how to do this exactly in theHoloEverywhere lib, which basically does the same for creating a holo theme backported to Android 2.2 or so

Finally, drop the Activity.setTheme(android.R.style.light); stuff, and just set your own theme via the manifest.




回答2:


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

Code for the first activity (the caller):

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

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.




回答3:


The attributes you want change are:

<style name="AppTheme" parent="android:style/Theme.Holo">
   <item name="android:textColorPrimary">...</item>
   <item name="android:textColorSecondary">...</item>
</style>

There are a few other ways to change them: Please Refer to this information

You can then set this Theme via the Manifest or setTheme(R.style.AppTheme) in your Activity's onCreate(...)



来源:https://stackoverflow.com/questions/14276550/change-whole-applications-text-color-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!