How to change R.string.some_variable at run time?

↘锁芯ラ 提交于 2020-01-13 19:31:09

问题


How to change R.string.app_name at run time?

I finding out my Android app's version from manifest file. Now I want to get this string R.string.app_name from res folder. I want to append my app's version with this app_name on the run time.

Are we allowed to do so in Android?


回答1:


No, this is not possible as all R.. resources are defined as constants in your R.java file . These are not edited by Android later on.




回答2:


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.

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.

If you are trying to store a value in a persistent way, take a look at SharedPreferences



来源:https://stackoverflow.com/questions/13933388/how-to-change-r-string-some-variable-at-run-time

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