资源文件values/strings.xml中添加
<string name="current_language">English</string>
<string name="test">test</string>
创建values-en文件夹,添加资源文件
strings.xml:
<resources>
<string name="current_language">English</string>
<string name="test">test</string>
</resources>
创建values-zh文件夹,添加资源文件
strings.xml
<resources>
<string name="current_language">Chinese</string>
<string name="test">测试</string>
</resources>
MainActivity layout中添加测试控件
<TextView
...
text = "@string/userid"
...
/>
Main Activity 中添加代码:
public class MainActivity ...
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
updateResources(this,"zh");
...
}
}
private static boolean updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return true;
}