【问题标题】:Android 5.0 API 21 application files contain AppCompat referencesAndroid 5.0 API 21 应用程序文件包含 AppCompat 引用
【发布时间】:2016-07-01 14:52:12
【问题描述】:

我正在努力学习 Android 应用程序源的结构。我在 Android Studio 中创建了一个新项目。我想使用材料设计的最新功能,此时我不考虑与早期 Android 版本的兼容性。所以我选择了最低 SDK - “API 21: Android 5.0 (Lollipop)”。然后我选择了“全屏活动”。第一个例外是“渲染问题。找不到以下类:android.support.v7.internal.app.WindowDecorActionBar”,当我尝试打开activity_fullscreen.xml 时。在 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 android-actionbar material-design android-5.0-lollipop


    【解决方案1】:

    为什么要从依赖项中删除 appcompat 和设计支持库? 这就是您收到构建错误的原因,因为

    Theme.AppCompat.Light.DarkActionBar
    

    Widget.AppCompat.ActionBar    
    

    在 appcompat 库中可用。

    【讨论】:

    • 因为正如我所描述的,我只想使用 Android 5.0 Lollipop API 21 提供的最新 UI 功能,而不提供任何兼容性。
    【解决方案2】:

    这可能是因为Android Studio没有基于API级别的不同模板,它只是到处使用AppCompat。

    Widget.AppCompat.ActionBar 替换为Widget.ActionBar 甚至更好,删除FullscreenActionBarStyle 及其所有引用并使用系统默认值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 2017-11-07
      • 1970-01-01
      相关资源
      最近更新 更多