Android 9。

发布于 2025-01-29 15:54:33 字数 1596 浏览 2 评论 0原文

这是我切换应用程序语言的代码:

  public static void changeLanguageApp(Context context, Locale locale) {
        Resources res = context.getResources();
        Configuration config = res.getConfiguration();
        Locale currLocale = getLocale(config);
        Configuration conf;
        if (currLocale != locale) {
            Locale.setDefault(locale);
            conf = new Configuration(config);
            conf.setLocale(locale);
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
    }

当应用程序运行时,开关效果很好。但是,如果我关闭应用程序然后运行它,则有设备的语言,而不是应用程序的语言。当我运行应用程序时,我会使用此代码:

public void onCreate() {
  super.onCreate();
  loadResources(this);
}

    public static Context loadResources(Context context) {
     return changeLocale(context, this, getResources().getConfiguration().locale);
    }

public static Context changeLocale(Context context, Locale locale) {
    Locale currentLocation = new Locale(locale.getLanguage());
    Locale.setDefault(currentLocation);
    Resources res = context.getResources();
    Configuration config = new Configuration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(currentLocation);
        context = context.createConfigurationContext(config);
        res.updateConfiguration(config, res.getDisplayMetrics());
    } else {
        config.locale = locale;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
    return context;
}

在调试中,我会看到,该应用程序语言检测到了正确的,但是我不明白,为什么不设置应用程序。 这种情况在Android 9和一些Android 11设备上。

This is my code for switching app language:

  public static void changeLanguageApp(Context context, Locale locale) {
        Resources res = context.getResources();
        Configuration config = res.getConfiguration();
        Locale currLocale = getLocale(config);
        Configuration conf;
        if (currLocale != locale) {
            Locale.setDefault(locale);
            conf = new Configuration(config);
            conf.setLocale(locale);
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
    }

when app is running switching works good. But if I close app and then run it, there are language of device, not of app. When I run my app I use this code:

public void onCreate() {
  super.onCreate();
  loadResources(this);
}

    public static Context loadResources(Context context) {
     return changeLocale(context, this, getResources().getConfiguration().locale);
    }

public static Context changeLocale(Context context, Locale locale) {
    Locale currentLocation = new Locale(locale.getLanguage());
    Locale.setDefault(currentLocation);
    Resources res = context.getResources();
    Configuration config = new Configuration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(currentLocation);
        context = context.createConfigurationContext(config);
        res.updateConfiguration(config, res.getDisplayMetrics());
    } else {
        config.locale = locale;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
    return context;
}

In debug I see, that app language detected right, but I dont understend, why doesn't it set for app.
Such situation is on Android 9 and some devices Android 11.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文