如何更改android中动态内容的语言

时间:2016-10-27 07:09:43

标签: java android

我必须这样做:

  • 用户应该能够输入文字来创建帖子 - "创建帖子"按钮
  • 这些帖子应该在墙上查看。
  • 根据所选语言,应用程序上的静态内容和动态内容应相应更改。它应该在应用程序的设置屏幕中可用。

我遇到了如何更改MainActivity上内容视图语言的问题

1 个答案:

答案 0 :(得分:1)

您可以像这样更改静态内容。 首先根据语言创建字符串文件。 当更改语言执行此方法时

 public void changeLang(String lang)
{
    myLocale = new Locale(lang);
    saveLocale(lang);
    Locale.setDefault(myLocale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = myLocale;
    getActivity().getApplicationContext().getResources().updateConfiguration(config, null);
    //getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

    updateTexts();

  }

private void updateTexts()
    {
    //update text of label in here.
   textLabel.setText(R.string.welcome);

    }

接下来是配置更改方法

@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (myLocale != null){
        newConfig.locale = myLocale;
        Locale.setDefault(myLocale);
        getActivity().getBaseContext().getResources().updateConfiguration(newConfig, getActivity().getBaseContext().getResources().getDisplayMetrics());
    }
}

我不知道动态内容。我认为这是不可能的。但是如果你有其他语言的内容,你可以在更改语言时加载。