如何要求android从指定文件中获取字符串?

如何要求android从指定文件中获取字符串?,android,Android,我将string.xml放入了值ar、值fr的文件中,我想手动从其中一个文件中获取字符串值。 例如: R.string-ar.... R.string-fr.... 我知道系统会根据系统语言或应用程序语言搜索字符串。 但是我想从指定的文件中获取字符串,而不考虑系统或应用程序语言 给你 Locale current = getResources().getConfiguration().locale; Locale newlocale = new Locale("fr"); setLocale

我将string.xml放入了值ar、值fr的文件中,我想手动从其中一个文件中获取字符串值。 例如:

R.string-ar....
R.string-fr....
我知道系统会根据系统语言或应用程序语言搜索字符串。 但是我想从指定的文件中获取字符串,而不考虑系统或应用程序语言

给你

Locale current = getResources().getConfiguration().locale;

Locale newlocale = new Locale("fr");
setLocale(newlocale );
String value = getResources().getString(R.string.username);//fr string
setLocale(current );


public void setLocale(Locale locale){
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
      getBaseContext().getResources().getDisplayMetrics());
}

这是以编程方式更改语言环境,但您需要在每次创建活动中调用它

Resources res = context.getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(language_code.toLowerCase());
res.updateConfiguration(conf, dm);

我认为您必须将区域设置设置为FR或AR,然后获取字符串,否则,我认为您必须自己解析XML值xx文件。不,我不想在我的应用程序内切换本地!这可能是无效的。更改本地不会影响视图(活动)\n除非重新加载或重新启动应用程序,这样您就可以设置区域设置get value,回滚到第一个区域设置并保留它。这是一种黑客行为,但我也会这样做-我想不出其他方法。@Yazan您能写一些代码吗!已进行编辑,Locale.setDefault(Locale);函数的第一行是newlocale(错误)Yazan你能看看我的问题吗?