问题
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