【问题标题】:app namespace not found in styles.xml in res folder在 res 文件夹的 styles.xml 中找不到应用命名空间
【发布时间】:2015-03-01 20:40:11
【问题描述】:

我正在使用 android.support.v7.widget.Toolbar 小部件编写自己的工具栏,我想将尽可能多的内容放入我的 res 文件夹中的 styles.xml。

/res/layout/$example.xml 中文件的一部分

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbar_show_addresses_simple"
    app:style="@style/toolbar_dark" >

我的“toolbar_dark”定义如下 /res/values/styles.xml

<style name="toolbar_dark">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@color/myPrimary</item>
    <item name="app:theme">@style/ThemeOverlay.AppCompat.Dark</item>
    <item name="app:popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="app:contentInsetStart">0dp</item>
</style>

编译时

  Output:
     Error: No resource found that matches the given name: attr 'app:contentInsetStart'.
     Error: No resource found that matches the given name: attr 'app:popupTheme'.
     Error: No resource found that matches the given name: attr 'app:theme'.

如果我直接在 $example.xml 中使用 app:* 值,一切正常。 因此,如何在 res 文件夹中的文件中使用我的应用命名空间?

【问题讨论】:

  • 使用 style="@style/toolbar_dark" 代替 app:style="@style/toolbar_dark"。从样式中删除应用前缀。
  • 首先尝试 Atheas 的建议,因为我也从未见过带前缀的样式属性。但是,我也从来没有让带有 app 前缀的元素在样式中工作。即使在我的样式文件中添加了xmlns:app=... 命名空间声明。
  • 您可以使用android命名空间,将app替换为android
  • @OneWorld 只需删除 styles.xml 中的应用命名空间就可以了 :-) 我不得不承认,我真的很困惑如何在 /res/*.xml 文件中声明某些内容...
  • @Athena 在样式前删除app: 是正确的声明方式,但对我的错误没有影响。

标签: android android-studio android-appcompat


【解决方案1】:

你不能在你的样式文件中使用应用命名空间,你应该在你的布局中引用style属性没有应用命名空间。

你可以这样做:

<android.support.v7.widget.Toolbar    
       xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/toolbar_show_addresses_simple"
        style="@style/toolbar_dark" >

风格:

<style name="toolbar_dark" parent="Widget.AppCompat.Toolbar">
    <item name="android:background">@color/green</item>
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="theme">@style/ThemeOverlay.AppCompat.Dark</item>
</style>

【讨论】:

  • 谢谢,删除“app”命名空间效果很好。当然,样式前面的“app:”也不是必需的。我真的需要一些睡眠 :-) 是否有某种文档说明在将它们从 /layout/* 文件移动到 /res/* 文件时以何种方式声明哪些属性?我应该为此提出一个新问题吗?
  • 这个解决方案对我不起作用。虽然我也有同样的问题
【解决方案2】:

在样式文件中使用Android替换app

<style name="toolbar_dark">
   <item name="android:layout_width">match_parent</item>
   <item name="android:layout_height">wrap_content</item>
   <item name="android:background">@color/myPrimary</item>
   <item name="android:theme">@style/ThemeOverlay.AppCompat.Dark</item>
   <item name="android:popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
   <item name="android:contentInsetStart">0dp</item>
</style>

【讨论】:

  • 它也适用于完全删除应用程序命名空间。使用 android 命名空间有什么好处吗?
  • 对于Android 3.0及更高版本需要android命名空间,看style actionbar
  • 我没有使用操作栏,我使用的是工具栏(没有 setSupportActionBar(toolbar); )使用支持库它工作得很好。如果我在没有支持库的情况下使用属性,我想我需要android 命名空间:-)
  • 没有帮助,仅删除应用命名空间在 styles.xml 文件中可以正常工作。
猜你喜欢
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-11
  • 1970-01-01
相关资源
最近更新 更多