Error:(27) No resource identifier found for attribute 'srcCompat' in package 'com.example.jaisonjoseph.newsclient'

Error:(27) No resource identifier found for attribute 'srcCompat' in package 'com.example.jaisonjoseph.newsclient'

我在 CardView 中添加了 ImageButton,当我在 ImageButton 中添加 app:srcComapat 时。我收到如下错误:

Error:(27) No resource identifier found for attribute 'srcCompat' in package 'com.example.jaisonjoseph.newsclient'

这是我的content_main.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.jaisonjoseph.newsclient.MainActivity"
    tools:showIn="@layout/app_bar_main"
    style="@style/Base.Widget.AppCompat.ButtonBar"
    android:background="#ffffff">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="350dp"
        android:layout_height="150dp"
        app:cardCornerRadius="6dp"
        android:background="#f6f6f6"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/daily"
            android:id="@+id/imageButton3"
            android:src="@drawable/daily" />
    </android.support.v7.widget.CardView>
</RelativeLayout>

RelativeLayout属性

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:app="http://schemas.android.com/apk/lib/com.example.jaisonjoseph.newsclient"

编辑:

添加这个

xmlns:app="http://schemas.android.com/apk/lib/com.example.jaisonjoseph.newsclient"

有关更多详细信息,请访问:

完整代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/lib/com.example.jaisonjoseph.newsclient"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.jaisonjoseph.newsclient.MainActivity"
        tools:showIn="@layout/app_bar_main"
        style="@style/Base.Widget.AppCompat.ButtonBar"
        android:background="#ffffff">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="350dp"
        android:layout_height="150dp"
        app:cardCornerRadius="6dp"
        android:background="#f6f6f6"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/daily"
            android:id="@+id/imageButton3"
            android:src="@drawable/daily" />
    </android.support.v7.widget.CardView>
</RelativeLayout>

你得到的似乎只是一个可以忽略的 lint 错误。

您可以使用:

tools:ignore="MissingPrefix"

在您的 RelativeLayout 中以避免暂时看到此错误。

因为srcCompat属性实际上是在AppCompat库中定义的,所以记得将AppCompat库添加到你的项目中。


更新
另外reader如何登陆本帖。

不要使用应用程序的名称空间使用项目名称,例如:

xmlns:app="http://schemas.android.com/apk/res/com.yourproject.name"

改为使用:

xmlns:app="http://schemas.android.com/apk/res-auto" .

正如https://code.google.com/p/android/issues/detail?id=9656#c71中所说:

"Added support for custom views with custom attributes in libraries. Layouts using custom attributes must use the namespace URI schemas.android.com/apk/res-auto instead of the URI that includes the app package name. This URI is replaced with the app specific one at build time."

https://code.google.com/p/android/issues/detail?id=9656

阅读更多内容