更新到 androidx.appcompat:appcompat:1.1.0 后的语言更改问题

Language change issue after updating to androidx.appcompat:appcompat:1.1.0

最新app-compat的link即1.1.0

将我的应用程序升级到最新的 app-compat 后,我的语言设置在 API 24 以下的手机上停止工作(大致上,不适用于 API 21 及以下肯定)。

对于 API 24 及以上版本,我使用了 ContextWrapper 并设置了 locale 因此有效。

我的问题是,如果 androidx.appcompat:appcompat:1.1.0 是稳定版本,为什么它在 alphabeta 版本中对我有用,这与这里的其他版本和我尝试过的问题不同。

Should I wait for an official stable version again and downgrade to the last stable version or which is the efficient way to let google know if any(ofcourse, I know to file a bug)?

Android x 已支持此类库 ...

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation "com.google.android.material:material:1.1.0-alpha09"

}

请始终更改设计库或其他 som 默认 android 库中的 androidx。

并且布局代码中的强制更改类似于...

<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_views"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:clipToPadding="false"
            android:padding="2dp" />


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

布局中还有更多其他类型代码更改...

编辑:

要继续使用 1.1.0 版,请在您的 attachBaseContext 下方添加:

Kotlin 解决方案:

override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
    if (overrideConfiguration != null) {
        val uiMode = overrideConfiguration.uiMode
        overrideConfiguration.setTo(baseContext.resources.configuration)
        overrideConfiguration.uiMode = uiMode
    }
    super.applyOverrideConfiguration(overrideConfiguration)
}

Java 解决办法:

@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
    if (overrideConfiguration != null) {
        int uiMode = overrideConfiguration.uiMode;
        overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
        overrideConfiguration.uiMode = uiMode;
    }
    super.applyOverrideConfiguration(overrideConfiguration);
}

If you don't need to upgrade to the latest appCompat then check the old answer. Else use the solution provided by @0101100101

旧答案:

经过数小时的尝试,得知这可能是一个错误。

降级到最后一个稳定版本,它可以完美运行。

dependencies {
    implementation 'androidx.appcompat:appcompat:1.0.2'   //************ DO NOT UPGRADE LANGUAGE ISSUE on API 23 and below *******************//
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
....
}

与此同时,我已经向 Google https://issuetracker.google.com/issues/140880275

提交了一个问题

新的应用兼容库中存在与夜间模式相关的问题,导致覆盖 android 21 到 25 上的配置。 这可以通过在调用此 public 函数时应用您的配置来解决:

public void applyOverrideConfiguration(Configuration overrideConfiguration

对我来说,这个小技巧通过将设置从覆盖的配置复制到我的配置而起作用,但你可以做任何你想做的事。最好将你的语言逻辑重新应用到新的配置中,以尽量减少错误

@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT <= 25) {
        //Use you logic to update overrideConfiguration locale
        Locale locale = getLocale();//your own implementation here
        overrideConfiguration.setLocale(locale);
}
super.applyOverrideConfiguration(overrideConfiguration);
}

要在最新的 appcompat v1.2.0 中正常使用区域设置更改,请使用以下代码。

(1) 将此函数添加到您的基础 activity。

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(setDefaultLanguage(newBase)); // Selected app language.
    applyOverrideConfiguration(newBase.getResources().getConfiguration());
}

注意:不要在 activity.

中覆盖此方法“applyOverrideConfiguration”