Android - How can i select a strings.xml without Localization

耗尽温柔 提交于 2021-02-11 01:43:49

问题


Is it possible to manually select a strings.xml file without changing the language of your device?

I would like to have Button to switch between two languages, but all i found is to use the build-in feature of android studio to create different strings.xml, which are selected by your localization.


回答1:


If you want to use a different strings.xml you have to switch localization. This can be done programmatically but is highly not recommended because you will mess with the system.

You can create a different resource XML file that you can put your strings there, give them a unique id and fetch them in the same way (R.id.your_string)

EDIT:

You create an XML file that you put into /res/values. This file will declare some strings in the same way the strings.xml does. With ids, values and everything. Then from the code what you do is to conditionally change strings. For example :

if (this){ //get the string from strings.xml here (R.strings.your_string else { //get the string from your_file.xml here the same way you did above. {

For more info check the documentation.



来源:https://stackoverflow.com/questions/31098089/android-how-can-i-select-a-strings-xml-without-localization

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