最新的app-compat
的链接为1.1.0。
将我的应用程序升级到最新的app-compat
后,我的语言设置就不再适用于 API 24 以下的手机(肯定不能在API 21及以下版本上使用)。
对于API 24及更高版本,我使用了ContextWrapper
并设置了locale
,因此有效。
我的问题是androidx.appcompat:appcompat:1.1.0
是否是稳定版本,为什么它在alpha
和beta
版本中对我有用,这与本文其他版本和我尝试过的问题不同。 >
1.1.0
)我应该再次等待正式的稳定版本并降级到 最后一个稳定的版本,或者这是让Google高效的方法 知道是否有(当然,我知道要提交错误)?
答案 0 :(得分:9)
编辑:
要继续使用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);
}
如果您不需要升级到最新的
appCompat
,请检查 旧答案。其他使用@ 0101100101提供的解决方案 here.
旧答案:
经过数小时的尝试,才知道这可能是一个错误。
降级到最后一个稳定版本,它可以正常工作。
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提交了问题
答案 1 :(得分:4)
新的应用兼容库中存在与夜间模式相关的问题,该问题导致覆盖android 21至25上的配置。 可以通过在调用此公共函数时应用配置来解决此问题:
public void applyOverrideConfiguration(配置overlayConfiguration
对我来说,此小技巧通过将设置从覆盖的配置复制到我的配置中而起作用,但是您可以执行任何所需的操作。最好将您的语言逻辑重新应用于新配置,以最大程度地减少错误
@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);
}
答案 2 :(得分:2)
要在最新的 appcompat v1.2.0 中更好地更改区域设置,请使用以下代码。
(1) 将此功能添加到您的基础活动中。
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(setDefaultLanguage(newBase)); // Selected app language.
applyOverrideConfiguration(newBase.getResources().getConfiguration());
}
注意:不要在您的活动中覆盖此方法“applyOverrideConfiguration”。
答案 3 :(得分:-1)
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">
还有更多其他一些类型代码在布局中发生了变化...