Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中检索字符串资源时显式指定语言?_Java_Android_Android Resources - Fatal编程技术网

Java 在android中检索字符串资源时显式指定语言?

Java 在android中检索字符串资源时显式指定语言?,java,android,android-resources,Java,Android,Android Resources,如何获得对语言中字符串资源的访问权限,不同于用户当前的语言环境 String string = context.getString(R.string.string_id, "en"); 您可以设置应用程序配置。使用以下命令: Locale locale = new Locale("en"); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; context

如何获得对语言中字符串资源的访问权限,不同于用户当前的语言环境

String string = context.getString(R.string.string_id, "en");

您可以设置应用程序配置。使用以下命令:

Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, null);
然后您可以使用:

String string = context.getResources().getString(R.string.string_id);

祝您好运。

您通常会在加载活动布局之前修改语言。 此外,您必须创建一个“value en”克隆文件夹,该文件夹中的“value”是res中的一个。在该文件夹中,您可以使用相同的名称将相同的字符串和资源放入该文件夹中,但要翻译成另一种语言。当您将这些克隆文件夹创建到“res”文件夹中时,您可以在确定活动的语言后以经典方式调用资源。看起来是这样的:

try{  
    Configuration c = new Configuration(getResources().getConfiguration());
    String lingua = readFileAsString("/sdcard/Lingua.txt");
    if(lingua.equals("")){ 
        c.locale = Locale.ITALIAN;
    }else if(lingua.equals("ITALIANO")){
        c.locale = Locale.ITALIAN;
    }else if(lingua.equals("ENGLISH")){ 
        c.locale = Locale.US;
    }
    getResources().updateConfiguration(c, getResources().getDisplayMetrics());

}catch(Exception e){ }

setContentView(R.layout.main);
opzioni[0] = getResources().getString(R.string.home);
opzioni[1] = getResources().getString(R.string.indietro);
opzioni[2] = getResources().getString(R.string.disattiva_audio);
opzioni[3] = getResources().getString(R.string.chiudi);
opzioni[4] = getResources().getString(R.string.ripeti);
在本例中,我将语言从文本文件恢复到SD卡中,并在对布局收费之前设置语言。正如您所看到的,getResources()没有指定语言。。。但它会转到带有后缀的文件夹克隆,后缀解释了正确的语言。

可能重复的