无法解析符号'Theme.Material3.Light.NoActionBar' (Android Studio)



我使用Android Studio Arctic Fox|2020.3.1 Patch 3创建了一个新的Android项目,我的目标是简单地自定义我的应用程序以使用Material3主题

values\themes.xml中出现错误,表示Cannot resolve symbol 'Theme.Material3.Light.NoActionBar'

我在这个问题上发现了一个类似的问题,但'Theme.MaterialComponents.Light.NoActionBar'是我创建项目时的默认主题,它没有错误,根据从M2迁移到M3(MDC 1.4.0到MDC 1.5.0(一节中迁移到材料设计3的文章,当我改为使用Theme.Material3.Light.NoActionBar时,错误就开始了

我尝试过用渐变文件同步项目,使缓存无效/重新启动。。但我仍然得到相同的错误

我已经使用材料主题生成器生成了我的themes.xmlcolors.xml,下面是我的文件

themes.xml

<resources>
<style name="Theme.App" parent="Theme.Material3.Light.NoActionBar">
<item name="colorPrimary">@color/md_theme_light_primary</item>
<item name="colorOnPrimary">@color/md_theme_light_onPrimary</item>
<item name="colorPrimaryContainer">@color/md_theme_light_primaryContainer</item>
<item name="colorOnPrimaryContainer">@color/md_theme_light_onPrimaryContainer</item>
<item name="colorSecondary">@color/md_theme_light_secondary</item>
<item name="colorOnSecondary">@color/md_theme_light_onSecondary</item>
<item name="colorSecondaryContainer">@color/md_theme_light_secondaryContainer</item>
<item name="colorOnSecondaryContainer">@color/md_theme_light_onSecondaryContainer</item>
<item name="colorTertiary">@color/md_theme_light_tertiary</item>
<item name="colorOnTertiary">@color/md_theme_light_onTertiary</item>
<item name="colorTertiaryContainer">@color/md_theme_light_tertiaryContainer</item>
<item name="colorOnTertiaryContainer">@color/md_theme_light_onTertiaryContainer</item>
<item name="colorError">@color/md_theme_light_error</item>
<item name="colorErrorContainer">@color/md_theme_light_errorContainer</item>
<item name="colorOnError">@color/md_theme_light_onError</item>
<item name="colorOnErrorContainer">@color/md_theme_light_onErrorContainer</item>
<item name="android:colorBackground">@color/md_theme_light_background</item>
<item name="colorOnBackground">@color/md_theme_light_onBackground</item>
<item name="colorSurface">@color/md_theme_light_surface</item>
<item name="colorOnSurface">@color/md_theme_light_onSurface</item>
<item name="colorSurfaceVariant">@color/md_theme_light_surfaceVariant</item>
<item name="colorOnSurfaceVariant">@color/md_theme_light_onSurfaceVariant</item>
<item name="colorOutline">@color/md_theme_light_outline</item>
<item name="colorOnSurfaceInverse">@color/md_theme_light_inverseOnSurface</item>
<item name="colorSurfaceInverse">@color/md_theme_light_inverseSurface</item>
<item name="colorPrimaryInverse">@color/md_theme_light_primaryInverse</item>
</style>
</resources>

build.gradle(应用程序(

plugins {
id 'com.android.application'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.blablabla"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildToolsVersion '32.0.0 rc1'
}
dependencies {
implementation 'androidx.compose.material3:material3:1.0.0-alpha02'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
testImplementation 'junit:junit:'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

builded.gradle(项目(

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.4"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

您必须在渐变文件中更改材料设计的依赖关系。

注意:为了使用新的Material3主题和组件样式,您必须依赖1.5.0-alpha04或更高版本。

开始使用MDC安卓

访问Google的Maven Repository或MVN Repository以查找该库的最新版本。

您必须使用alpha版本的材料依赖

Alpha发布-这是当您正在开发的功能不完整或部分完整时发布的版本。

更新您的材料依赖性

implementation 'com.google.android.material:material:1.4.0'

implementation 'com.google.android.material:material:1.6.0-alpha01'
  1. 从渐变中删除:runtimeOnly组:"androidx.appcompat",名称:"appcompat",版本:"1.7.0-alpha01"实现'com.google.android.material:material:1.7.0'
  2. 同步
  3. 添加:runtimeOnly组:"androidx.appcompat",名称:"appcompat",版本:"1.7.0-alpha01"实现'com.google.android.material:material:1.7.0'
  4. 再次同步

更新您的材料依赖性

implementation 'com.google.android.material:material:1.4.0'

implementation 'com.google.android.material:material:1.7.0-rc01'

只要更仔细地重新检查Gradle依赖项,之间就有区别

implementation 'androidx.compose.material3:material3:1.2.0-alpha04'

implementation 'com.google.android.material:material:1.11.0-alpha01'

虽然Jetpack Compose需要第一个,但Android MDC需要第二个。添加第二个将修复您的问题和主题。Material3.Light.NoActionBar可以从theme.xml文件访问。

相关内容

最新更新