【发布时间】:2016-12-23 03:52:37
【问题描述】:
我想使用 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 doesn't work with v7 AppCompat library
在这里你可以下载我的项目:
https://drive.google.com/file/d/0BxiDy9-wQHvzNFRhaTMzelg1WlU/view?usp=sharing
(抱歉英语不好,欢迎指正)
【问题讨论】:
-
为了更好地理解问题,粘贴代码。
-
你在哪里应用主题?在你的
AndroidManifest.xml?尝试核对bin/obj文件夹,清理您的项目并重建。还请确保您的v7 AppCompatNuGet 包被正确引用。 -
@JonDouglas 我正在应用
AndroidManifest.xml中的主题。我核对了bin和obj文件夹。我清理并重建了我的对象。我得到了和以前一样的错误:Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'.(更多细节在我的问题顶部)。当我点击重建时,我收到了这个错误:1> java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0(更多细节在我的问题的编辑(2)部分)。所以不幸的是它没有用:( -
对于
Unsupported major.minor version 52.0错误,请尝试卸载当前的 JDK 版本并安装 1.8。如果您不断收到错误,请上传示例项目并使用链接编辑您的帖子。 -
@JonDouglas 在我重建项目时不再出现错误,但在我清理项目时仍然出现同样的错误。
标签: c# android xml xamarin xamarin.android