以编程方式更改从API响应中获得的颜色资源的价值 [英] Programmatically change the value of a color resource obtained from API response

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

问题描述

让我们说,在我的API调用我有一个这就是所谓的颜色参数。是否可以编辑或修改存在的 R.colors.color 来分配从API结果的颜色?

作为一个例子:

我对我的API的调用和返回绿色,现在我想载入我的应用程序,即(绿色工具栏绿色的TextView 颜色等),这可能吗?

我的第一个想法是:

上创建 colors.xml 名为演示然后将其指定一个默认颜色的项目,然后用这个演示彩的地方我想(按钮的TextView 等)后来我想这可能是可能与来自API的结果编程修改这个值,所以我不会需要创建一个共享preferences 或类似的东西和为了避免更多的code。

由于 @ Y.S。对我说:


  

不幸的是,你必须设置文本的颜色或手动查看到处... :(


我想,如果有其他的方式来做到这一点,因为我不知道有多少活动我的项目将包含,所以如果有其他办法可以做到它,我很高兴听到其他的猜测。

修改

我想的@Jared Rummler答案,也许是我做错了什么......我创建了一个简单的的Json ,我把我的我的资产解析的Json ,我把它放在 GlobalConstant 然后我做了一个简单的应用程序。

首先我有一个的TextView 按钮其中包含your_special_color,而回归它我把 GlobalConstant INT 如下:

 案your_special_color:
            返回GlobalConstant.color;

那我试过是我的第一个活动有1 的TextView 1 按钮如我之前所说,他们有颜色your_special_color我不想改变它,但我有一个意图我的按钮来打开其他活动包含相同,但与 GlobalConstant.color 和它不发生变化。

我试过这样做(我的第二个活动):

 公共类Main2Activity扩展AppCompatActivity {
私人RES资源;
@覆盖公共资源getResources(){
    如果(RES == NULL){
        RES =新的RES(super.getResources());
    }
    返回水库;
}
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main2);
    工具条工具栏=(栏)findViewById(R.id.toolbar);
    setSupportActionBar(工具栏);
}

我错过了什么?

呵呵..我想通了我的猜测是,在这样做我的 MainActivity2

 按钮BTN =(按钮)findViewById(R.id.button2);
 btn.setBackgroundColor(res.getColor(R.color.your_special_color));


解决方案

如果你看看在的访问资源文件,它说的是...


  

一旦你在你的应用提供资源,你可以通过引用它的资源ID应用它。所有的资源ID在你的项目的研究类,其中 AAPT 工具自动生成定义。


此外,


  

当您的应用程序的编译 AAPT 生成研究类,
  它包含了所有在 RES / 资源资源标识
  目录。对于每种类型的资源,有一个研究子类。(
  例如, R.drawable 所有绘制资源),并为每个
  该类型的资源,有一个静态的整数(例如,
   R.drawable.icon )。此整数,你可以使用的资源ID
  找回你的资源。


这是什么话,本质上,是几乎一切都持有一个的资源的在 RES / 目录编译并引用为一个不可改变不变。正是出于这个原因,资源要素的值不能以编程方式更改/在运行时,因为他们的编译。相对于局部/全局变量和放大器; 共享preferences ,资源元素被重新程序存储器作为固定的,不可改变的对象psented $ P $。它们在程序存储器中一个特殊只读regiom保持。在这方面,亦见 R.String编程的变化值

您的什么可以的做的是,要避免使用相同的code。在项目中的千地方,创建改变了<$ C $颜色的值的公共函数C>共享preferences 到处都使用这种方法。我敢肯定,你知道这已经,当然。

要减少$ C $的量c,你需要添加到项目中,还存在另一种。我有previously使用这让我解决的字体样式和放大器书法库;颜色整个应用程序。这可能是一些良好的使用给你,看看...

Let's say, on my API call I have a parameter that's called color. Is it possible to edit or modify an existent R.colors.color to assign the color from the API result?

As an example:

I make a call to my API and it returns green, now I want to load my app with i.e (green Toolbar, green TextView color, etc.), is that possible?

My first thought was:

Create a item on colors.xml called demo then assign it a default color, then use this demo color wherever I want (Button, TextView, etc.) Then I thought it could be possible to change this value programmatically with the result from the API so I wouldn't need to create a SharedPreferences or something like that and for avoiding more code.

As @Y.S. said to me

Unfortunately, you WILL have to set the color of the text or view manually everywhere ... :(

I would like if there is other way to do it, since I don't know how many Activities my project will contain, so if is there other way to do it I'm glad to hear other guesses.

EDIT

I'm trying the @Jared Rummler answer and maybe i'm doing something wrong... I've created a simple Json and I put on my Assets I parse the Json and I put it on a GlobalConstant then I made a "simple app".

First of all I have a TextView and a Button which contains the "your_special_color", and the return of it I put the GlobalConstant int as follows :

case "your_special_color":                
            return GlobalConstant.color; 

Then what I tried is my first Activity has 1 TextView and 1 Button as I said before and they have the color "your_special_color" that I don't want to change it, BUT I have an Intent on my Button to open the other Activity that contains the same but with the GlobalConstant.color and it doesn't change.

I tried it doing this (my second Activity):

public class Main2Activity extends AppCompatActivity {
private Res res;
@Override public Resources getResources() {
    if (res == null) {
        res = new Res(super.getResources());
    }
    return res;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

Did I miss something?

Oh.. I figured it out I guess is doing this on my MainActivity2 ?

 Button btn = (Button)findViewById(R.id.button2);
 btn.setBackgroundColor(res.getColor(R.color.your_special_color));

解决方案

If you take a look at the Accessing Resources document, what it says is that...

Once you provide a resource in your application, you can apply it by referencing its resource ID. All resource IDs are defined in your project's R class, which the aapt tool automatically generates.

Furthermore,

When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory. For each type of resource, there is an R subclass (for example, R.drawable for all drawable resources), and for each resource of that type, there is a static integer (for example, R.drawable.icon). This integer is the resource ID that you can use to retrieve your resource.

What this is saying, essentially, is that practically everything held as a resource in the res/ directory is compiled and referenced as an unchangeable constant. It is for this reason that the values of resource elements cannot be changed programmatically/at runtime, because they are compiled. As opposed to local/global variables & SharedPreferences, resource elements are represented in program memory as fixed, unchangeable objects. They are held in a special read-only regiom of program memory. In this regard, see also Changing value of R.String Programmatically.

What you can do is, to avoid using the same code at a thousand places in your project, create a common function that changes the value of the color in the SharedPreferences and use this method everywhere. I'm sure you knew this already, of course.

To reduce the amount of code you need to add to the project, there is an alternative. I have previously used the calligraphy library which allowed me to fix the font style & color throughout the app. This may be of some good use to you, check it out ...

这篇关于以编程方式更改从API响应中获得的颜色资源的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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