【问题标题】:The attribute is not declared (Android) on basic xml attributes该属性未在基本 xml 属性上声明 (Android)
【发布时间】:2016-05-05 16:32:48
【问题描述】:

我是这个 Android 开发的新手,我发现它的布局真的很混乱。我正在尝试在视图上添加背景图像,并且尝试使用此示例 Add a background image to shape in xml Android,但是看起来很糟糕(你知道的位图)

所以我认为向量可能很有趣。唯一的问题是我什至无法让官方示例正常工作。我试过把它设置为背景

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="256dp"
    android:width="256dp"
    android:viewportWidth="32"
    android:viewportHeight="32">
<path android:fillColor="#8fff" android:pathData="M20.5,9.5
                  c-1.955,0,-3.83,1.268,-4.5,3
                  c-0.67,-1.732,-2.547,-3,-4.5,-3
                  C8.957,9.5,7,11.432,7,14
                  c0,3.53,3.793,6.257,9,11.5
                  c5.207,-5.242,9,-7.97,9,-11.5
                  C25,11.432,23.043,9.5,20.5,9.5z" />

它在设计视图和所有视图中呈现,但属性viewportWidthviewportHeightfillColorpathData 都显示相同的警告:

The 'http://schemas.android.com/apk/res/android:viewportWidth' is not declared

如果我检查文件,果然,它不存在。这是否意味着我必须明确声明所有这些类型?香草的例子似乎有点奇怪。

请注意,如果我删除警告前面的“android”,它会删除警告但仍然给我同样的部署错误

Android.Views.InflateException: Binary XML file line #1: Error inflating class <unknown>

【问题讨论】:

  • 您正在运行 Visual Studio 的 Xamarin 的哪个版本?哪个版本的Visual Studio?哪个Android SDK 版本?
  • @SushiHangover Visual studio 2015 14.0.25123, Xamarin 4.0.3, Xamarin.Android 6.0.3.5 看来我安装的最新SDK是API 22 (5.1.1)
  • @SushiHangover 我现在已经升级到了最新的 SDK,但是很遗憾,什么都没有改变

标签: android xml xamarin


【解决方案1】:

问题

Intellisense 无法选择我们键入的属性,尽管这些属性存在于 android SDK 中并显示此属性未声明。

解决方案

我昨天在 Visual Studio 2015 中遇到了这个问题并开始搜索这个问题,最终我发现这两个文件在 Visual Studio 的 XML 架构文件夹中丢失了,所以下载下面给出的这些文件链接,

  1. https://github.com/atsushieno/monodroid-schema-gen/blob/master/android-layout-xml.xsd
  2. https://github.com/atsushieno/monodroid-schema-gen/blob/master/schemas.android.com.apk.res.android.xsd

只需手动下载文件并将其移动到 C:\Program Files (x86)\Microsoft Visual Studio 14.0\Xml\Schemas 或直接在 Visual Studio 中添加这些架构。这个错误会消失,我用这个程序解决了这个问题。

【讨论】:

  • 我认为这是最好的解决方案。为我工作。
  • 刚刚安装了 VS 2017。希望这会得到解决。同样的问题。
  • 在vs2017中你应该把这两个文件放在%projectDirectory%/obj\Debug\Schemas\19下
  • 你必须添加这两个文件。在 VS 2015 的工具栏中,选择 XML -> Schemas.. -> 添加。我在VS 2017中检查了这个问题,那里没有发生错误。
  • 这个答案中的文件似乎已经过时了。
【解决方案2】:

据我所知,那些属性(你在上面指出的)没有在 jdk_1.7 中定义

改用 jdk_1.8。

您可以通过以下方式配置JDK的路径:

Microsoft Visual Studio 2015 -> 工具 -> 选项 -> Xamarin -> Android 设置 -> JDK 位置 [更改]

备注

如果您使用的是 Microsoft Visual Studio 2015 Update 3 或早期版本之一,我向您保证警告不会消失...这已经是 Intellisense 的问题(不是 JDK 的问题)。它必须在未来的版本中修复。

【讨论】:

  • 我仍然收到警告“属性未声明”,但现在项目运行。在我无法部署之前
  • @Banana,很高兴能帮到你!但是警告 不会 消失...这已经是 Intellisense 的问题(不是 JDK 的)。它必须在未来的版本中修复。
  • 我的使用 jdk1.8.0_101,它仍然给我这个警告,并且在设计模式下无法正确显示。你知道为什么会这样吗?
  • @Minh 我已经切换到原生 Android Studio。 Xamarin 仍然是绿色和错误的。
  • @Andrey-WD 该问题特定于 Xamarin for VS 插件。 Xamarin Studio 没有这个问题。
【解决方案3】:

创建一个基于模板的Xamarin.Android 应用程序。

从 Google/Android VectorDrawable 文档中获取 vector.xml 示例,并将其创建为项目中 Resources/drawable 目录下的 .xml 文件:

<?xml version="1.0" encoding="UTF-8" ?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
     android:height="64dp"
     android:width="64dp"
     android:viewportHeight="600"
     android:viewportWidth="600" >
     <group
         android:name="rotationGroup"
         android:pivotX="300.0"
         android:pivotY="300.0"
         android:rotation="45.0" >
         <path
             android:name="v"
             android:fillColor="#000000"
             android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
     </group>
 </vector>

更新您的Resouce/layout/Main.axml 以在LinearLayoutButton 的背景上包含矢量:

<?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"
        android:background="@drawable/vector"
        >
    <Button
        android:id="@+id/myButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:background="@drawable/vector"
        />
</LinearLayout>

结果:

Google 的例子不是很好,但它确实有效。

清理你的矢量并将颜色设置为红色

<?xml version="1.0" encoding="UTF-8" ?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
     android:height="256dp"
     android:width="256dp"
     android:viewportHeight="60"
     android:viewportWidth="60" >
     <group
         android:name="rotationGroup"
         android:pivotX="0.0"
         android:pivotY="0.0"
         android:rotation="0.0" >
        <path
            android:name="so"
            android:fillColor="#F44336"
            android:pathData="M20.5,9.5
              c-1.955,0,-3.83,1.268,-4.5,3
              c-0.67,-1.732,-2.547,-3,-4.5,-3
              C8.957,9.5,7,11.432,7,14
              c0,3.53,3.793,6.257,9,11.5
              c5.207,-5.242,9,-7.97,9,-11.5
              C25,11.432,23.043,9.5,20.5,9.5z" />
     </group>
 </vector>

结果:

【讨论】:

  • 我也试过了,问题是visual studio无法识别标签属性。我已经编辑了我的问题并上传了一个屏幕截图,这样你就可以看到我在说什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多