在res文件夹的styles.xml中找不到应用程序命名空间。

37

我正在使用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" >

在/res/values/styles.xml中,我的"toolbar_dark"被定义如下:

<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文件夹中的文件中使用我的应用程序命名空间?


如果我直接在$example.xml中使用"app:*"的值,一切都正常。那么,我该如何在res文件夹中的文件中使用我的应用程序命名空间?

1
在样式中使用style="@style/toolbar_dark"代替app:style="@style/toolbar_dark"。从样式中删除app前缀。 - Athena
首先尝试Atheas的建议,因为我从未见过带有前缀的样式属性。然而,即使在我的样式文件中添加了xmlns:app=...命名空间声明,我也从未能够让带有app前缀的元素在样式内起作用。 - OneWorld
你可以使用 android 命名空间,将 app 替换为 android - Apurva
@OneWorld 只需在 styles.xml 中删除应用程序命名空间即可解决 :-) 我必须承认,我真的很困惑如何在 /res/*.xml 文件中声明某些内容... - Felix Fiskare
@Athena 去掉样式前面的 app: 是正确的声明方式,但这并没有影响我的错误。 - Felix Fiskare
2个回答

69

你不能在样式文件中使用应用程序命名空间,而且在布局中引用 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>

7
谢谢,删除 "app" 命名空间成功了。当然,在 style 前面加上 "app:" 也是不必要的。我真的需要好好休息一下 :)在将属性从 /layout/* 文件移动到 /res/* 文件时应以何种方式声明哪些属性?是否有某种文档介绍这方面的内容?我需要为此提一个新的问题吗? - Felix Fiskare
这个解决方案对我不起作用。虽然我有完全相同的问题。 - Renaud Favier

-5

在样式文件中使用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”命名空间有什么好处吗? - Felix Fiskare
对于 Android 3.0 及更高版本,需要使用 android 命名空间,请查看 样式操作栏 - Xcihnegn
我没有使用ActionBar,而是使用了一个Toolbar(没有setSupportActionBar(toolbar);)。使用支持库可以正常工作。如果我不使用支持库的属性,我想我需要android命名空间 :-) - Felix Fiskare
在styles.xml文件中,只需删除应用程序命名空间即可,这样做很有效,但并不是很有帮助。 - xarlymg89

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接