【问题标题】:Android Custom actionBarStyle compatibility errorAndroid自定义actionBarStyle兼容性错误
【发布时间】:2014-12-25 13:17:05
【问题描述】:

我正在尝试学习如何在 Android 中进行开发,我正在学习本教程:https://developer.android.com/training/basics/actionbar/styling.html

现在,我正在尝试将操作栏的背景设置为橙色。 本教程提供了以下代码(请注意,我正在使用 Android 2.1 兼容性进行开发,因此我使用的是 AppCompat 等)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item> <!-- error here -->

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>

        <!-- Support library compatibility -->
        <item name="background">@drawable/actionbar_background</item>
    </style>
</resources>

我的 actionbar_background.xml 文件在哪里:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#FFA500" />
</shape>

我的问题是我收到以下错误:android:actionBarStyle requires API level 11 (current min is 8) 在我添加评论 &lt;!-- error here --&gt; 的行中。

我找到的解决方案是删除该行。也因为它看起来与下面的行正在做的事情完全相同(除了没有明确说明“android:”命名空间)。现在似乎一切正常...

我想知道这是否是教程代码中的错误(即我删除该行是正确的)或者我是否有其他类型的问题......有什么想法吗?谢谢!

编辑

以下帖子解决了同样的问题:Android AppCompat requires API level 11

看来这确实是教程本身的错误。不过,我仍然不清楚,为什么问题发生在 android 命名空间(应该特别关注 API>14)而不是 默认一个(应该解决任何其他 API)。

【问题讨论】:

    标签: android android-layout android-actionbar android-actionbar-compat


    【解决方案1】:

    这是因为 AppCompat 主题仅适用于 sdk 11。要么您需要更改主题,要么您应该将 Manifest 文件中的 min sdk 版本从 8 更改为 11。

    <uses-sdk
        android:minSdkVersion="8"/>
    
    <uses-sdk
            android:minSdkVersion="11"/>
    

    编辑

    你只能使用:

    <!--For Api 8-->
    <item name="actionBarStyle">Style ref </item> 
    <!--For Api 11 or more-->
    <item name="android:actionBarStyle">Style ref </item> 
    

    您可能会收到错误消息,您的名称空间“android:actionBarStyle”仅适用于 API LEVEL 11 及更高版本。

    如果您希望能够将 ActionBar 的样式设置为在所有 API 级别中看起来都相同,您需要为选定的 api 级别创建不同的文件夹,并在这些文件夹中创建新的 style.xml/themes.xml 文件。例如:

    - res
      -- values
         -- styles.xml
         -- themes.xml // API LEVEL 8+
     -- values-v11
         -- styles.xml
         -- themes.xml // API LEVEL 11+
     -- values-v14
         -- styles.xml
         -- themes.xml // API LEVEL 14+
    

    【讨论】:

    • 嗯,你确定只有这样吗?在我的 build.gradle 文件中,我有:编译“com.android.support:appcompat-v7:21.0.3”。我想我的兼容主题应该从版本 7 开始工作。这也无法解释为什么当我删除该行时它会起作用,尽管我的 minSdkVersion 仍然是 8...
    • 我确信这一点。我对 gradle 的工作了解不多,但我在 Eclipse 上工作并多次遇到类似的问题
    • 好的,谢谢。我用更多信息编辑了我的答案。或许你知道为什么问题只发生在 android 命名空间中?
    • 好的,谢谢。现在更清楚了,尽管我仍然有点困惑......我仍然不明白为什么我需要使用“android:”命名空间,而在这种情况下,默认命名空间似乎对所有 API 都适用。我想当我有更多的编程经验时我会更好地理解......:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    相关资源
    最近更新 更多