簡體   English   中英

Xamarin-檢索項目的父項時出錯:找不到與給定名稱'Theme.AppCompat.Light.DarkActionBar'匹配的資源

[英]Xamarin - Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'

我想使用Xamarin和Visual Studio使用Material Design創建一個應用程序。 我想使用v7 AppCompat庫來實現此目的,因此我的應用在舊設備上可以很好地運行。

我遵循了本教程:

https://blog.xamarin.com/android-tips-hello-material-design-v7-appcompat/

並完全一樣。 當我轉到Main.axml文件(在Resources/layout文件夾中)時,有一個下拉菜單,您可以在其中選擇主題(下圖)。 但是,當我打開下拉菜單時,我的主題沒有出現。 因此,我認為清理並重建項目是一個好主意。 但是當我清理項目時,出現了以下錯誤:

Error retrieving parent for item: No resource found that matches the given
name 'Theme.AppCompat.Light.DarkActionBar'.

No resource found that matches the given name: attr 'colorAccent'.
No resource found that matches the given name: attr 'colorPrimary'.
No resource found that matches the given name: attr 'colorPrimaryDark'.
No resource found that matches the given name: attr 'windowActionBar'.
No resource found that matches the given name: attr 'windowNoTitle'.

如何修復它並使主題出現在下拉菜單中?

編輯:

這是代碼:

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
    android:id="@+id/MyButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Hello" />
</LinearLayout>

values/styles.xml

<resources>
  <style name="MyTheme" parent="MyTheme.Base">
  </style>
  <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">#2196F3</item>
    <item name="colorPrimaryDark">#1976D2</item>
    <item name="colorAccent">#FF4081</item>
  </style>
</resources>

values-v21

<resources>
  <style name="MyTheme" parent="MyTheme.Base">
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
  </style>
</resources>

MainActivity.cs

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace MaterialDesign
{
    [Activity(Label = "MaterialDesign", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 1;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);

            Button button = FindViewById<Button>(Resource.Id.MyButton);
            button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
        }
    }
}

注意:

另外,當我使用android.support.v7.widget.Toolbar元素時,出現以下錯誤: android.support.v7.widget.Toolbar element is not declared

編輯(2):

我嘗試使用組件管理器和NuGet命令(軟件包管理器控制台)下載它,然后手動下載dll並引用它們,但是仍然無法正常工作。 我還刪除了\\AppData\\Local\\Xamarin\\Android.Support.v7.AppCompat\\23.0.1.3文件夾並刪除了緩存,但是沒有成功。

在設計器選項卡的Main.axml文件中,顯示警告:

This project contains resources that were not compiled successfully,
rendering might be affected

我發現問題出在Visual Studio Designer,因為它可以在手機上正常運行。 所以我問了一個新問題: Xamarin.Android,Visual Studio-Designer無法與v7 AppCompat庫一起使用

在這里您可以下載我的項目:

https://drive.google.com/file/d/0BxiDy9-wQHvzNFRhaTMzelg1WlU/view?usp=sharing

(對不起,英語不好,請隨時糾正我)

它與代碼無關。 只是在撰寫本文時,Visual Studio不支持自定義視圖。

只需使用Xamarin Studio或此插件https://visualstudiogallery.msdn.microsoft.com/9f5a516a-f4d0-4228-9d25-d0273abebf33 ,即可在Android Studio中編輯應用程序。

有關更詳細的答案請參見Xamarin.Android,Visual Studio-Designer不與v7 AppCompat庫一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM