【发布时间】:2017-08-19 23:47:38
【问题描述】:
我会说我已经在 stackoverflow 上看到了这个问题的一些答案,但只提供了一个快速的“修复”解决方案。如果可能的话,我还想了解为什么会发生这种情况(带有一些细节的答案)。
现在来回答这个问题:我刚刚使用 Android Studio 2.2 重新开始了 android 开发。我有一个要移植到 android 的 ios 应用程序(意思是,为 Android 重新创建它)。我从 Android Studio 的基本模板开始,使用复制到 DRAWABLE 文件夹中的 2 个 PNG 文件添加了一个约束布局和 2 个 ImageView。
在不做任何更改或任何事情的情况下,当我尝试构建时出现此错误:
错误:(11) 在包“x.y.z”中找不到属性“srcCompat”的资源标识符。两个图像都会发生这种情况。这是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/lib/x.y.z"
xmlns:app2="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="x.y.z.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app2:srcCompat="@drawable/pngFile1"
android:id="@+id/imageView"
app2:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="8dp"
app2:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app2:srcCompat="@drawable/pngFile2"
android:id="@+id/imageView2"
app2:layout_constraintBottom_toTopOf="@+id/imageView"
app2:layout_constraintRight_toRightOf="@+id/imageView"
android:layout_marginBottom="8dp" />
现在,为了正确回答我的问题,我想了解以下几点:
-
我在 SO 上找到的一些其他答案建议更改默认值:
xmlns:app="http://schemas.android.com/apk/res-auto"
到:
xmlns:app="http://schemas.android.com/apk/lib/x.y.z"
确实,这消除了构建中的错误,但对我来说似乎很奇怪(我几乎没有 android 经验)是 Android Studio 在我添加第二个图像时创建了另一个类似的行:
xmlns:app2="http://schemas.android.com/apk/res-auto"
正如我们所见,它使用它来处理图像,因此错误再次出现。
我记得一年前我在 Android Studio 上开发了一个应用程序,并且使用几乎相同的方式添加图像/带有图像的按钮,并且这些问题不存在。
这就是为什么我也想了解为什么会发生这种情况以及如何正确解决它。
提前致谢!
【问题讨论】:
-
我有一个项目我已经做了一年多,添加图像总是正常的,没问题。最近我发现了“约束布局”,所以我开始使用,我放入了一些 imageViews,然后出现了你提到的同样的问题。这里的区别是“srcCompat”——它为什么改变了?似乎将 App.Compat 更新到最新版本可以解决问题,我还没有这样做,因为我对以前的版本有一些依赖。
-
看看这里,它可能会给你一些背景知识——我们使用的是约束布局的预览版,它可以解释一些奇怪的行为sites.google.com/a/android.com/tools/recent
标签: android png android-studio-2.2