You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Android 8.1中?attr/actionBarSize与?attr/colorPrimary资源未定义

Xamarin Android:?attr/actionBarSize和?attr/colorPrimary未定义问题解决

问题场景

首次使用Xamarin开发Android应用,header_layout.xml中引用的?attr/actionBarSize和?attr/colorPrimary被IDE提示未定义,相关属性已在styles.xml、colors.xml、dimens.xml中配置,但错误提示依然存在。

相关文件代码

header_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="vertical"
    android:padding="16dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary">
    <!-- Hamburger icon -->
    <ImageView
            android:id="@+id/hamburger_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/baseline_menu_open_24"
            android:contentDescription="Open Navigation Drawer"/>
    </android.support.v7.widget.Toolbar>
    
    <!-- Add header content -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Header"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <!-- Add more views or controls as needed -->

</LinearLayout>

styles.xml(原配置)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Material">
         <!-- Customize your theme here. -->
        
    <!-- Change the background color -->
    <item name="android:windowBackground">@color/background_color</item>
    <!-- Define the actionBarSize attribute -->
    <item name="android:actionBarSize">@dimen/actionBarSize</item>
    <!-- Define the colorPrimary attribute -->
    <item name="android:colorPrimary">@color/colorPrimary</item>
    <item name="android:colorControlNormal">@color/colorControlNormal</item>
    </style>
</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#2c3e50</color>
    <color name="colorPrimaryDark">#1B3147</color>
    <color name="colorAccent">#3498db</color>
    <color name="colorControlNormal">#555555</color>
    <!-- Define your custom background color -->
    <color name="background_color">#FFA500</color>
</resources>

dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Define the actionBarSize attribute -->
    <dimen name="actionBarSize">56dp</dimen>
    <!-- You can adjust the value as needed -->
</resources>

解决步骤

1. 修正主题继承与属性命名空间

你使用的是android.support.v7.widget.Toolbar,这类V7支持库控件依赖AppCompat主题,且对应的属性不需要android:前缀:

  • 将styles.xml中的主题父类改为Theme.AppCompat.Light.NoActionBar(自定义Toolbar时需禁用系统默认ActionBar)
  • 移除actionBarSize、colorPrimary、colorControlNormal属性的android:前缀

修改后的styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
         <!-- Customize your theme here. -->
        
    <!-- Change the background color -->
    <item name="android:windowBackground">@color/background_color</item>
    <!-- Define the actionBarSize attribute -->
    <item name="actionBarSize">@dimen/actionBarSize</item>
    <!-- Define the colorPrimary attribute -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorControlNormal">@color/colorControlNormal</item>
    </style>
</resources>

2. 确认主题全局应用

检查AndroidManifest.xml,确保application标签已指定使用修正后的主题:

<application 
    android:label="YourAppName" 
    android:theme="@style/AppTheme">
    <!-- 其他配置内容 -->
</application>

3. 清理重建项目

Visual Studio的缓存可能导致错误提示残留,执行以下操作:

  • 点击顶部菜单 生成 > 清理解决方案
  • 再点击 生成 > 生成解决方案
  • 重启Visual Studio并重新加载项目

4. 验证支持库引用

确保项目已安装Xamarin.Android.Support.v7.AppCompat包:

  • 右键项目 → 管理NuGet程序包
  • 在浏览标签页搜索Xamarin.Android.Support.v7.AppCompat,确认已安装;未安装则点击安装

内容的提问来源于stack exchange,提问作者Dyonun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 00:37:47