Override res/values/strings.xml file with /strings

2020-07-24 05:50发布

I want to replace strings.xml file which is embedded into the apk, by a strings.xml file downloaded from server and stored into my internal sd card. Is it possible to reference external tags to TextView. My clients want to have complete control over languages in the application. If i successfully download strings.xml from server and stored it to sd card, how can i reference the tag element to respective TextViews. I don't really need code but only ideas and critism about this method.

标签: android
2条回答
欢心
2楼-- · 2020-07-24 06:24

This is not possible. If you want to achieve this, you will have to set the language in runtime while creating your controls.

If you have complete layouts (not creating dynamic controls), you will have to recursively scan your layouts, looking for buttons, textviews, hints etc., recognize the control and set the text property from the downloaded file.

something like this:

put your language in a hashmap:

HashMap<String, String> LANG = new HashMap<String, String>();

and set the tag property for each of your controls in your layout. Then you can find them like this:

for(int i=0;i<yourLayout.getChildsCount;i++)
{
    View v = yourLayout.getChildAt(i);
    if(v instanceof Button){
        ((Buttons)v).setText(LANG.get(v.getTag()));
    }
}
查看更多
3楼-- · 2020-07-24 06:34

I don't think this will work because your resources get compiled into the apk in a special way so that they can be accessed based on the device configuration. This happens at compile time. You can't simply swap out the strings.xml later while the app is installed.

查看更多
登录 后发表回答