在方向配置更改时更改语言环境配置

时间:2019-03-14 12:38:00

标签: android

我通过这种方法根据用户的选择来强制使用我的android应用程序上使用的语言

   public static void setLanguage(MySharedPreferences mySharedPreferences, Context context) {

    mySharedPreferences = MySharedPreferences.getInstance(context);

    Locale locale = new Locale(mySharedPreferences.getLanguageKey());
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    context.getResources().updateConfiguration(config,
            context.getResources().getDisplayMetrics());
}

在整个应用程序中都能正常运行,但是在一个活动中,我遇到了一个问题,即当方向改变时,它会读取字符串值语言,具体取决于系统语言而不是应用程序语言,并且如果我将活动设置为类似清单

<activity
        android:name=".ProductDetailsActivity"
        android:exported="false"
        android:configChanges="orientation|screenSize"
        android:label="@string/product_confirm_page" />
    <activity

但这不是我要寻找的解决方案,因为无论方向如何变化,它都会呈现当前的布局设计,而不是针对我制作的每个方向都呈现自定义设计。

当我删除android:configChanges="orientation|screenSize"时 从清单中的活动标签中尝试使用方法 onConfigurationChanged 处理更改,该方法未针对方向更改和语言支持问题调用。

0 个答案:

没有答案
相关问题