R.string programically的变化值string、programically

2023-09-12 22:43:10 作者:玩腻

我已经声明了一个字符串,在我的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 .

推荐答案

一件事是什么,你要明白这里的是,当你提供一个数据作为一种资源,不能在运行时修改。例如,你有你的绘制文件夹中的可绘制不能在运行时修改。为了precise,在资源文件夹无法通过程序修改。

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,即价值的文件夹。如果在所有你想要一个字符串,它具有在运行时进行修改,创建一个单独的类,有你的字符串在运行时放置在这个类和访问。这是最好的解决方案,我发现。

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.