【问题标题】:Error:(27) No resource identifier found for attribute 'srcCompat' in package 'com.example.jaisonjoseph.newsclient'错误:(27) 在包“com.example.jaisonjoseph.newsclient”中找不到属性“srcCompat”的资源标识符
【发布时间】:2016-09-27 06:18:21
【问题描述】:

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

错误:(27) 未找到属性“srcCompat”的资源标识符 包'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:app2行。

标签: android xml resources android-relativelayout android-cardview


【解决方案1】:

只需更改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"

更多详情请访问:No resource identifier found for attribute ' ' in package 'com.app....'

完整代码:

<?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>

【讨论】:

  • schemas.android.com/apk/res/android" package="com.example.jaisonjoseph.newsclient">跨度>
  • 这是我的包名,可以编辑并给我正确的值@lroman
  • @Jaison_Joseph 尝试 cleanrebuild 项目。
  • @Jaison_Joseph 很高兴为您提供帮助。
  • 虽然这解决了操作问题,但这不是推荐的。而是使用xmlns:app="http://schemas.android.com/apk/res-auto" 。如前所述:“在库中添加了对具有自定义属性的自定义视图的支持。使用自定义属性的布局必须使用命名空间 URI schemas.android.com/apk/res-auto 而不是包含应用程序包名称的 URI。此 URI 将替换为应用程序特定的建造时间。”在code.google.com/p/android/issues/detail?id=9656#c71
【解决方案2】:

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

你可以使用:

tools:ignore="MissingPrefix"

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

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


更新
对于另一位读者如何登陆此线程。

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

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所说:

“在库中添加了对具有自定义属性的自定义视图的支持。使用自定义属性的布局必须使用命名空间 URI schemas.android.com/apk/res-auto 而不是包含应用程序包名称的 URI。此 URI 在构建时替换为特定于应用程序的 URI。"

阅读更多https://code.google.com/p/android/issues/detail?id=9656

【讨论】:

  • 我可以在哪里找到 AppCompat 库
  • 将以下代码添加到您的应用 build.gradle dependencies:com.android.support:appcompat-v7:24.2.1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多