【问题标题】:Errors When Setting Custom Android Theme设置自定义 Android 主题时出错
【发布时间】:2017-06-14 16:47:30
【问题描述】:

当我尝试通过 AndroidManifest 设置主题时出现错误。构建时的错误是“未找到与给定名称匹配的资源(在 'theme' 处,值为 '@styles/MainApplicationStyle')”

我已经清理了项目,并对其进行了多次重建。我还收到来自编译器的消息和警告,格式为“找不到元素的架构信息”(此处的属性)。所有这些消息和警告都来自 AndroidManifest。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          package="TheTipApp.TheTipApp" android:versionCode="1" 
          android:versionName="1.0" android:installLocation="auto">

    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="25" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application android:label="TheTipApp" />
    <application android:theme="@styles/MainApplicationStyle"/>
</manifest>

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MainApplicationStyle" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  </style>
</resources>

我也尝试在 AssemblyInfo.cs 文件中设置主题,不确定是否会造成问题。

AssemblyInfo.cs

[assembly: AssemblyTitle("TheTipApp")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TheTipApp")]
[assembly: AssemblyCopyright("Copyright ©  2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Application(Theme = "@styles/MainApplicationStyle")]

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

我也不确定这是否与此有关,但我也有 Xamarin.Android.Support.v7.AppCompat 和 Xamarin.Android.Support.v4 已安装。

【问题讨论】:

    标签: c# android xml xamarin


    【解决方案1】:

    您需要使用@style 而不是@styles。其次,您的AndroidManifest.xml 中应该只有一个&lt;application&gt; 节点

    只有&lt;manifest&gt; 和&lt;application&gt; 元素是必需的。它们每个都必须存在并且只能出现一次。

    来源:https://developer.android.com/guide/topics/manifest/manifest-intro.html#filec

    【讨论】:

    • 那是否意味着values\styles.xml也需要重命名为values\style.xml?
    • 不。您可以将style 放在任何名为.xml 的自定义文件中。 styles.xml 只是您将自定义主题放置在何处的约定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多