我會說我已經在stackoverflow上看到了這個問題的一些答案,但只提供了一個快速'修復'的解決方案。如果可能的話,我希望能夠理解爲什麼會發生這種情況(有一些詳細的答案)。在包''中找不到屬性'srcCompat'的資源標識符。'
現在來回答這個問題:我剛剛使用Android Studio 2.2重新開始了Android開發。我有一個ios應用程序,我想移植到Android(意思是,爲Android重新創建它)。我已經開始使用Android Studio中的基本模板,添加了一個約束佈局和2個使用2個PNG文件的ImageViews,這些文件已複製到DRAWABLE文件夾中。
而不進行任何更改或任何,當我嘗試建立我得到這個錯誤:
錯誤:(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" />
現在,這裏有一些事情,我想明白了,纔能有一個正確回答我的問題:
一些我找到的其他答案建議更改默認值:
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到最新版本可以解決這個問題,我還沒有做到這一點,因爲我有一些依賴於以前的版本。 –
在這裏看看它可能會給你一些背景 - 我們使用約束佈局的預覽版本,可以解釋一些奇怪的行爲https://sites.google.com/a/android.com/tools/recent –