Android 5.0 API 21 个应用程序文件包含 AppCompat 引用
Android 5.0 API 21 application files contain AppCompat references
我正在努力学习 Android 应用程序源的结构。我在 Android Studio 中创建了一个新项目。我想使用 material 设计的最新功能,此时我不考虑与早期 Android 版本的兼容性。所以我选择了 Minimum SDK - "API 21: Android 5.0 (Lollipop)"。然后我选择了"Fullscreen Activity"。当我尝试打开 activity_fullscreen.xml 时,第一个异常是 "Rendering problems. The following classes could not be found: android.support.v7.internal.app.WindowDecorActionBar"。在 build.gradle 中,我从 dependencies 中删除了最后两行:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
}
然后我得到一个构建错误:
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'.
我将 'styles.xml' 配置为使用 Material 主题而不是 "Theme.AppCompat.Light.DarkActionBar"
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:colorAccent">@color/colorAccent</item>
</style>
但仍然存在一个错误:
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'Widget.AppCompat.ActionBar'.
在操作栏样式定义中
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
<item name="android:background">@color/black_overlay</item>
</style>
我不知道如何在没有支持库的情况下设置操作栏。为什么我的项目确实需要它?我阅读的文档描述了仅使用 AppCompat 的操作栏设置。或者 Material 主题根本不提供操作栏?为什么自从我选择了最低 SDK 21 后源代码中会出现 appcompat-v7 引用?我真的很困惑,因为我什至无法构建最简单的项目。
为什么要从依赖项中删除 appcompat 和设计支持库?
这就是您遇到构建错误的原因,因为
Theme.AppCompat.Light.DarkActionBar
和
Widget.AppCompat.ActionBar
在 appcompat 库中可用。
可能是因为AndroidStudio没有基于API级别的不同模板,它只是到处使用AppCompat。
将 Widget.AppCompat.ActionBar
替换为 Widget.ActionBar
或更好,删除 FullscreenActionBarStyle
和所有对它的引用并使用系统默认值。
我正在努力学习 Android 应用程序源的结构。我在 Android Studio 中创建了一个新项目。我想使用 material 设计的最新功能,此时我不考虑与早期 Android 版本的兼容性。所以我选择了 Minimum SDK - "API 21: Android 5.0 (Lollipop)"。然后我选择了"Fullscreen Activity"。当我尝试打开 activity_fullscreen.xml 时,第一个异常是 "Rendering problems. The following classes could not be found: android.support.v7.internal.app.WindowDecorActionBar"。在 build.gradle 中,我从 dependencies 中删除了最后两行:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
}
然后我得到一个构建错误:
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'.
我将 'styles.xml' 配置为使用 Material 主题而不是 "Theme.AppCompat.Light.DarkActionBar"
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:colorAccent">@color/colorAccent</item>
</style>
但仍然存在一个错误:
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'Widget.AppCompat.ActionBar'.
在操作栏样式定义中
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
<item name="android:background">@color/black_overlay</item>
</style>
我不知道如何在没有支持库的情况下设置操作栏。为什么我的项目确实需要它?我阅读的文档描述了仅使用 AppCompat 的操作栏设置。或者 Material 主题根本不提供操作栏?为什么自从我选择了最低 SDK 21 后源代码中会出现 appcompat-v7 引用?我真的很困惑,因为我什至无法构建最简单的项目。
为什么要从依赖项中删除 appcompat 和设计支持库? 这就是您遇到构建错误的原因,因为
Theme.AppCompat.Light.DarkActionBar
和
Widget.AppCompat.ActionBar
在 appcompat 库中可用。
可能是因为AndroidStudio没有基于API级别的不同模板,它只是到处使用AppCompat。
将 Widget.AppCompat.ActionBar
替换为 Widget.ActionBar
或更好,删除 FullscreenActionBarStyle
和所有对它的引用并使用系统默认值。