android - 当我在android中更改应用程序中的语言环境时如何重新加载当前 View

标签 android android-layout android-fragments locale android-location

我目前正在处理更改应用程序中的语言。我的应用程序结构是 tab host + fragment 我已经成功更改了语言环境,但它很奇怪。

这意味着在我运行 change locale code 之后,它不会立即更改 View ,但只有在我转到另一个选项卡时才会更改。我认为这是因为我需要重新加载 View ,但是有什么方法可以在不终止并重新启动 Activity 的情况下实现这一点?

因为有一些goolge解析代码,如果用户再次启动 Activity ,条目数会增加吗?有重新加载 View 的标准方法吗?谢谢

更改语言环境功能在 tabhost fragment 之一中,我必须刷新 tabhost 中的 View (主要 Activity )和当前 fragment 。

public OnClickListener setChangeLangListener(final String lang) {
    OnClickListener changeLangListener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Configuration config = new Configuration(getResources()
                    .getConfiguration());

            if (lang.equals("en")) {
                config.locale = Locale.ENGLISH;
                chi.setTextColor(oldColor);
                eng.setTextColor(getActivity().getResources().getColor(android.R.color.white));
            } else {
                config.locale = Locale.TRADITIONAL_CHINESE;
                eng.setTextColor(oldColor);
                chi.setTextColor(getActivity().getResources().getColor(android.R.color.white));
            }

            getResources().updateConfiguration(config,
                    getResources().getDisplayMetrics());
        }
    };
    return changeLangListener;
}

eng.setOnClickListener(setChangeLangListener("en"));
        chi.setOnClickListener(setChangeLangListener("zh"));

最佳答案

好吧,把这个添加到你的 list 中

android:configChanges="locale"

并在您的 Activity 中覆盖 onConfigurationChanged()

@Override
public void onConfigurationChanged(Configuration newConfig) {
  // refresh your views here
  super.onConfigurationChanged(newConfig);
}

here获取更多信息。
希望能帮助到你。 :)

关于android - 当我在android中更改应用程序中的语言环境时如何重新加载当前 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21494009/

相关文章:

java - 相对布局参数无法正常工作

java - 如何在android上的另一个布局中为 TextView 分配一个值

java - 选项卡式 Activity 中 android fragment 中的可绘制错误

android - 一项 Activity 或单独 Activity 中的多个 fragment

java - 试图将 Activity 转换为 fragment

c# - 通过 C# Web Service 获取更新的 APK 版本并安装

android - 检测底部工作表对话框 fragment 在基础 fragment 上消失

android - FrameLayout 内的 TextView 被截断而不换行

java - 为什么 WebView -> onPause 只在第一次工作?

Android Studio 3.0 金丝雀 - 无法解析 : org. apache.httpcomponents :httpclient:4. 0.1