我正在努力学习Android应用程序源代码的结构。我在Android Studio中创建了一个新项目。我想使用材料设计的最新功能,并且在这一点上,我不考虑与早期Android版本的兼容性。因此,我选择了最低SDK-“ API 21:Android 5.0(Lollipop)”。然后,我选择了“全屏活动”。当我尝试打开activity_fullscreen.xml时,第一个例外是“呈现问题。找不到以下类:android.support.v7.internal.app.WindowDecorActionBar”。在build.gradle中,我从依赖项中删除了最后两行:
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参考?我真的很困惑,因为我什至无法构建最简单的项目。
最佳答案
可能是因为Android Studio没有基于API级别的不同模板,它只是在各处使用AppCompat。
将Widget.AppCompat.ActionBar
替换为Widget.ActionBar
或什至更好,删除FullscreenActionBarStyle
及其所有引用,并使用系统默认值。