android - 在运行时更改区域设置不影响 fragment

标签 android android-layout android-fragments locale

当用户更改语言时,我执行以下代码,它适用于 Activity 中的当前 fragment ,但如果我转到其他 fragment ,它会部分更新语言,一些字符串会更新并显示旧语言,最重要的是日期不会在内部 fragment 和其他 Activity 中发生变化。

我在牛轧糖、棉花糖和奥利奥中对此进行了测试,它在所有操作系统中都发生了。

当用户更改语言时,我将执行以下操作。

 LocaleHelper.setLocale(getApplicationContext(), language);
 recreate();

本地助手

public static Context setLocale(Context context, String language) {
        persist(context, language);


        Log.d("LocaleSet", language);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, language);
        }

        return updateResourcesLegacy(context, language);
    }

post marshmallow OS 的方法。

   @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Configuration configuration = context.getResources().getConfiguration();
        configuration.setLocale(locale);

        return context.createConfigurationContext(configuration);
    }

前牛轧糖

@SuppressWarnings("deprecation")
    private static Context updateResourcesLegacy(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLayoutDirection(locale);
        }

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());

        return context;
    }

在每个 Activity 中,我执行以下代码。

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleHelper.onAttach(base, LocaleHelper.getLanguage(base)));
}

主要节日

<application
        android:name=".GlobalApplication"
        android:allowBackup="false"
        android:icon="@mipmap/app_icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/app_icon"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:windowSoftInputMode="adjustPan"
        tools:replace="android:allowBackup">

        <activity
            android:name=".activities.homeactivity.HomeActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:configChanges="locale"
            android:theme="@style/AppTheme.NoActionBar" />

        <activity
            android:name=".activities.profilepageactivity.ProfilePageActivity"
            android:label="@string/title_activity_profile_page"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar" />

最佳答案

我假设您使用的是 this方法。它没有建议在 AndroidManifest.xml 中进行任何更改,因此 android:configChanges="locale" 可能会导致您定义的不当行为。

对于日期格式,您应该考虑到您的应用程序未使用 Locale.getDefault(),而是由用户和您的 LocaleHelper 定义的不同内容机制。

关于配置更改的一些额外细节

android:configChanges 意味着您不希望系统在提供的属性之一发生时重新创建您的 Activity 。在您的例子中是 locale

就使用该方法进行开发而言,为了正确处理 list 中的此选项,您必须实现

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

然后进行自己的处理。您的情况不需要的东西。

关于android - 在运行时更改区域设置不影响 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55669185/

相关文章:

java - Android MultiAutoCompleteTextView 具有自定义标记器,如 Whatsapp GroupChat

android - 如何通过旋转正确保留 DialogFragment?

java - ViewPager 中 Fragments 的长时间运行 AsyncTasks

android - 进度条 : set location/position in Canvas

android - 如何摆脱 CardView 中 ImageView 周围的空白

android - JSONObject 响应为空

android - 出现软键盘时布局不会向上推

android - 如何同时为android布局元素的背景使用形状和选择器?

android - 如何禁用布局内的所有 View ?

java - 我的静态上下文在哪里?