Jetpack Material Design 1.1.0版本后使用MaterialDesign控件闪退的问题
参考CSDN博客解决方案:升级到material:1.1.0以上版本后MaterialCardView,MaterialButton 等控件闪退的问题
以下是原文复制:
最简单的方法:
给控件属性加一行: android:theme="@style/Theme.MaterialComponents"
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="123dp"
android:theme="@style/Theme.MaterialComponents"/>
或者全局theme继承MaterialComponents下的Bridge主题(推荐该方法)
<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar.Bridge">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
这样就不会闪退了。
升级到material:1.1.0,可能会报 Error inflating class com.google.android.material.card.MaterialCardView 或 Error inflating class com.google.android.material.button.MaterialButton等错误,
这是因为material:1.1.0以后,部分Material Design组件需要MaterialComponents包下的theme才支持,而新建项目默认使用的还是Theme.AppCompat包,我们手动改成Theme.MaterialComponents即可。
例如新建项目,默认使用的theme为:Theme.AppCompat.Light.DarkActionBar,如果需要使用Material控件,则需要改成MaterialComponents包下的Light.DarkActionBar,即:Theme.MaterialComponents.Light.DarkActionBar
默认项目里,AndroidManifest里application节点的写法,注意,重点看android:theme="@style/AppTheme"
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
```xml
点进AppTheme,可以看到继承的父theme为Theme.AppCompat目录下的theme
```xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
注意,此时Theme.AppCompat下的主题是不支持MaterialCardView,MaterialButton 等控件的,需要改为Theme.MaterialComponents下的theme才可以,即修改为
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
之后再使用MaterialCardView,MaterialButton 等控件就不会报错了。
有时候,我们并不希望项目使用MaterialComponents下的样式,因为MaterialComponents样式的弹窗、Snackbar等会和AppCompat样式有很大不同,随意修改可能达不到我们预期的效果,如果有使用AppCompat样式的需要,我们也可以只修改目标控件下的theme值,以MaterialCardView为例,你可以把MaterialCardView的theme值改成MaterialComponents,即:
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="123dp"
android:theme="@style/Theme.MaterialComponents"/>
或者使用Bridge主题包(参考文章开头),使用Bridge主题可以在不改变程序原有样式的情况下,使用Material Design组件
官方给我们提供了以下Bridge主题包,可以根据自己需要做选择
Theme.MaterialComponents.Bridge
Theme.MaterialComponents.Light.Bridge
Theme.MaterialComponents.NoActionBar.Bridge
Theme.MaterialComponents.Light.NoActionBar.Bridge
Theme.MaterialComponents.Light.DarkActionBar.Bridge
这样,即使使用AppCompat,也不会闪退。
todo 待补充Android Studio报错的图片
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人