1.checkbox radioButton等自定义背景和自带背景重叠问题
androidx由于刚推出不久,当然也有一些无可避免的坑,这不,今天用低版本的手机(android4.4)一运行,发现所有的checkbox自带的按钮都显示出来了,在xml中是用"android:button=null"竟然不起作用了!
查看源码发现是
public class CheckBox extends CompoundButton {
public CheckBox(Context context) {
this(context, null);
}
public CheckBox(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.checkboxStyle);
}
public CheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public CheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public CharSequence getAccessibilityClassName() {
return CheckBox.class.getName();
}
}
构造方法里面添加了一个默认的主题,因此自己定义一个主题覆盖掉这个就可以了,因此有了下面的解决方案:
<style name="libs_AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="toolbarStyle">@style/libs_baseAppTheme
<item name="checkboxStyle">@style/noButtonCheckbox
<item name="radioButtonStyle">@style/noButtonRadioButton
</style>
<style name="noButtonCheckbox" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:background">@null
<item name="buttonCompat">@null
<style name="noButtonRadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
<item name="android:background">@null
<item name="buttonCompat">@null
</style>
对应问题解决
2.部分手机升级androidX后切换语言不生效问题
升级androidX一个多星期,忽然发现7.0以下部分手机却换语言不生效,但是想想似乎和升级没什么关系,毕竟这个AppCompatActivity
早就已经上线了的,因此先用排除法,打了一个升级androidx之前的包试了一下,果然可以切换。这TMD谷歌也坑人啊,改了啥也不提前说。
于是就去网上一顿查资料,查了半天别人遇到的问题都是显而易见的,分分钟都有解决方案。但是这个语言切换的没发现在哪里。
最后好不容易发现一个国外的帖子:
The link of the latestapp-compatwhich is 1.1.0.
After upgrading my app to the latestapp-compatmy language settings stopped working for phones belowAPI 24(roughly, doesn't work on API 21 and below for sure).
For API 24 and above, I have used theContextWrapperand set thelocalehence works.
My question is if theandroidx.appcompat:appcompat:1.1.0is the stable version why does it work for me inalphaandbetaversions unlike the others here & the questions which I have tried.
After updating AppCompat library to appcompat:1.1.0-alpha03 Locale configuration is not working anymore
Change Locale not work after migrate to Androidx - Talks about the alpha and beta (I am using the latest stable build1.1.0)
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)?
回答1:
Edit:
To continue using version 1.1.0 add this below yourattachBaseContext:
Kotlin solution:
overridefunapplyOverrideConfiguration(overrideConfiguration:Configuration?){if(overrideConfiguration !=null) {valuiMode = overrideConfiguration.uiMode overrideConfiguration.setTo(baseContext.resources.configuration) overrideConfiguration.uiMode = uiMode }super.applyOverrideConfiguration(overrideConfiguration)}
Java solution:
@OverridepublicvoidapplyOverrideConfiguration(Configuration overrideConfiguration){if(overrideConfiguration !=null) {intuiMode = overrideConfiguration.uiMode; overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration()); overrideConfiguration.uiMode = uiMode; }super.applyOverrideConfiguration(overrideConfiguration);}
If you don't need to upgrade to the latestappCompatthen check the old answer. Else use the solution provided by @0101100101 here.
Old Answer:
After spending hours trying, got to know that it might be a bug.
Downgradeto the last stable version and it works flawlessly.
dependencies{implementation'androidx.appcompat:appcompat:1.0.2'//************ DO NOT UPGRADE LANGUAGE ISSUEonAPI23and below *******************// implementation'androidx.legacy:legacy-support-v4:1.0.0'....}
Meanwhile, I have filed an issue with Google https://issuetracker.google.com/issues/140880275
回答2:
There is an issue in new app compat libraries related to night mode that is causing to override the configuration on android 21 to 25. This can be fixed by applying your configuration when this public function is called:
public void applyOverrideConfiguration(Configuration overrideConfiguration
For me, this little trick has worked by copying the settings from the overridden configuration to my configuration but you can do whatever you want. It is better to reapply your language logic to the new configuration to minimize errors
@OverridepublicvoidapplyOverrideConfiguration(Configuration overrideConfiguration){if(Build.VERSION.SDK_INT >=21&& Build.VERSION.SDK_INT <=25) {//Use you logic to update overrideConfiguration localeLocale locale = getLocale();//your own implementation hereoverrideConfiguration.setLocale(locale);}super.applyOverrideConfiguration(overrideConfiguration);}
握草,我刚好是1.1.0,完美入坑。赶紧按照这个解决方案在baseActivity里面重写了这个方法
@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
// 兼容androidX在部分手机切换语言失败问题
if (overrideConfiguration !=null) {
int uiMode = overrideConfiguration.uiMode;
overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
overrideConfiguration.uiMode = uiMode;
}
super.applyOverrideConfiguration(overrideConfiguration);
}
3. Android WebView 5.x 系统下 Resources$NotFoundException异常处理
之前没有见过这个错误,自从升级androidx 到appCompat1.1.0之后,vivo手机5.X一直在崩溃
最近线上后台发现一个崩溃问题,在Android5.x上,创建webview时会发生carsh,报错信息:
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003 at android.content.res.Resources.getText(Resources.java:318) at android.content.res.VivoResources.getText(VivoResources.java:123) at android.content.res.Resources.getString(Resources.java:404) at com.android.org.chromium.content.browser.ContentViewCore.setContainerView(ContentViewCore.java:694) at com.android.org.chromium.content.browser.ContentViewCore.initialize(ContentViewCore.java:618) at com.android.org.chromium.android_webview.AwContents.createAndInitializeContentViewCore(AwContents.java:631) at com.android.org.chromium.android_webview.AwContents.setNewAwContents(AwContents.java:780) at com.android.org.chromium.android_webview.AwContents.(AwContents.java:619) at com.android.org.chromium.android_webview.AwContents.(AwContents.java:556) at com.android.webview.chromium.WebViewChromium.initForReal(WebViewChromium.java:312) at com.android.webview.chromium.WebViewChromium.access$100(WebViewChromium.java:96) at com.android.webview.chromium.WebViewChromium$1.run(WebViewChromium.java:264)
在大部分的vivo 5.x系统下会出现问题,非5.x系统不会出现。
解决办法一
在Android5.x上通过解决自定义WebView
网上大部分都是这种方式解决的。
publicclassLollipopFixedWebViewextendsWebView{publicLollipopFixedWebView(Contextcontext){super(getFixedContext(context));}publicLollipopFixedWebView(Contextcontext,AttributeSetattrs){super(getFixedContext(context),attrs);}publicLollipopFixedWebView(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(getFixedContext(context),attrs,defStyleAttr);}@TargetApi(Build.VERSION_CODES.LOLLIPOP)publicLollipopFixedWebView(Contextcontext,AttributeSetattrs,intdefStyleAttr,intdefStyleRes){super(getFixedContext(context),attrs,defStyleAttr,defStyleRes);}publicLollipopFixedWebView(Contextcontext,AttributeSetattrs,intdefStyleAttr,booleanprivateBrowsing){super(getFixedContext(context),attrs,defStyleAttr,privateBrowsing);}publicstaticContextgetFixedContext(Contextcontext){if(Build.VERSION.SDK_INT>=21&&Build.VERSION.SDK_INT<23)// Android Lollipop 5.0 & 5.1returncontext.createConfigurationContext(newConfiguration());returncontext;}}
主要核心方法是getFixedContext(context),根据版本号配置不同的context.
解决方法二
如果项目中使用"androidx.appcompat:appcompat:1.1.0",替换为"androidx.appcompat:appcompat:1.0.2"
1.1.0版本webview在Android5.x上有问题,恢复到1.0.2之后确实解决了此问题,目前没有具体追踪,后续会跟上。
4.最终使用androidx.appcompat:appcompat:1.1.0-rc01版本解决语言切换和webview崩溃问题。
5.忽然之前的1.1.0-rc01切换语言和webview崩溃问题重新出现,查看了代码版本号还是之前的啊,代码都没改动,这是啥情况啊?一脸懵逼啊
最后在打包的时候Android左边External Library看到有两个appcompat:appcompat,一个是androidx.appcompat:appcompat:1.1.0-rc01一个是androidx.appcompat:appcompat:1.1.0。这个1.1.0是哪里来的?最终对比代码提交记录发现是'com.google.android.material:material:1.1.0'这个下面引入的包。结果android studio就指向1.1.0正式版啦。卧槽!这他妈能不能制定一个版本号啊,最后找到一个设置
// 指定androidx.appcompat使用版本号,
// 此处是为了兼容1. 7.0以下部分手机切换语言失败问题 2. 5.X系统部分手机webview找不到资源问题
configurations.all{
resolutionStrategy{
resolutionStrategy.eachDependency{ details->
if (details.requested.group =='androidx.appcompat') {
details.useVersion"1.1.0-rc01"
}
}
}
}
重新打包测试,可以啦。记得这个设置放到app下面build.gradle里。
到此问题解决!
暂时分享到这里,其他的遇到了在分享吧。