以编程方式更改R.string的值 [英] Change value of R.string programmatically

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

问题描述

我在我的strings.xml文件中声明了一个字符串,并在我的活动中将其用作R.string.compose_title. (将其设置为标题,即setTitle(R.id.compose_title)).现在在某些情况下,我想编辑字符串,然后使用它来设置标题.我该怎么办?

I have declared a string in my strings.xml file , and using it in my activity as R.string.compose_title. (setting it as title i.e. setTitle(R.id.compose_title)). Now in some case I want to edit the string and then use it to set the title . How can I do this ?

P.S.我只需要更改单个字符串的值,因此使用本地化为每种情况(根据用户而定)声明一个新的strings.xml似乎效率不高.

P.S. I need to change value of a single string only , So declaring a new strings.xml for each case(which are variable depending upon the user) using localization seems to be a lil inefficient .

推荐答案

在这里您需要了解的一件事是,当您将数据作为资源提供时,无法在运行时对其进行修改.例如,可绘制文件夹中的可绘制对象无法在运行时进行修改.确切地说,无法以编程方式修改"res"文件夹.

One thing what you have to understand here is that, when you provide a data as a Resource, it can't be modified during run time. For example, the drawables what you have in your drawable folder can't be modified at run time. To be precise, the "res" folder can't be modified programatically.

这也适用于Strings.xml,即"Values"文件夹.如果您根本想要一个在运行时必须修改的String,请创建一个单独的类,并将您的字符串放入该Class中,并在运行时进行访问.这是我发现的最佳解决方案.

This applies to Strings.xml also, i.e "Values" folder. If at all you want a String which has to be modified at runtime, create a separate class and have your strings placed in this Class and access during run time. This is the best solution what I have found.

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

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