在res文件夹的styles.xml中找不到app命名空间

Fel*_*are 34 android android-appcompat android-studio

我正在用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" >
Run Code Online (Sandbox Code Playgroud)

我的"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>
Run Code Online (Sandbox Code Playgroud)

编译时

  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'.
Run Code Online (Sandbox Code Playgroud)

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

Gab*_*tti 65

您不能在样式文件中使用app命名空间,并且应该style在布局中引用属性wihtout app namespace.

你可以像这样做:

<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" >
Run Code Online (Sandbox Code Playgroud)

样式:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你,删除"app"命名空间工作正常.当然,风格前面的"app:"也没有必要.我真的需要一些睡眠:-)是否有某种文件,当从/ layout/*文件移动到/ res/*文件时,以什么方式声明哪些属性?我应该为此开一个新问题吗? (6认同)