添加 ImageView 导致 srcCompat 属性出错

Adding ImageView Causes Error with srcCompat attribute

我有另一个 activity,其布局具有 RelativeLayout、TextViews 和 ImageView...这里没问题。

下面这个 Activity 我有很多 TextView 和一个 RelativeLayout。当我添加 Imageview 时,我收到以下错误:

Error:(190) No resource identifier found for attribute 'srcCompat' in package 'com.example.ftonyan.userposts' Error:(190) No resource identifier found for attribute 'srcCompat' in package 'com.example.ftonyan.userposts' Error:Execution failed for task ':app:processDebugResources'.

com.android.ide.common.process.ProcessException: Failed to execute aapt

这个有问题的 activity layout.xml 的顶部在添加 imageview 之前看起来如下(代码在模拟器上编译和运行)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

将图像视图添加到调色板后,以下两行是红色的(注意第二行是由系统添加的)。代码不再编译。

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

下面是我的Activitylayout.xml。此布局中大约有 19 个 textView。为了简洁起见,我没有包括所有这些。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">


    <TextView
        android:id="@+id/lblAddressees"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/lblNick"
        android:layout_toStartOf="@+id/nickName"
        android:text="Addressees:" />

    <Button
        android:id="@+id/goBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:background="@android:color/transparent"
        android:text="back" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button6"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="87dp"
        android:srcCompat="@mipmap/ic_launcher" />

</RelativeLayout>

我在这里阅读了一些建议更改 apk/res-auto 行的帖子。但是,在系统添加之前,该行最初不在文件中。所以,不确定我应该做什么来解决这个问题。提前致谢

您需要为 Imageview 添加命名空间代码

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

命名空间 'schemas.android.com/tools' 用于指定 Android Studio 构建应用程序的选项,不包含在最终应用程序包中

命名空间 'schemas.android.com/apk/res-auto' 用于所有自定义属性 - 在库或代码中定义。

在您的项目中打开应用级别 build.gradle 文件添加以下行

defaultConfig {
   vectorDrawables.useSupportLibrary = true
}

并同步您的项目。就是这样。

我明白了。我错过了这一行

tools:context="packagename.nameofAttivity">

谢谢大家

检查这个

在此 Post 正如我所说的 --- ...... 你应该将 srcCompat 编辑为 src。 src 用于源代码,sourceCompat 用于 Android 设备 运行 ICS 及以下设备的矢量绘图文件的向后兼容性 -- IceCream Sandwich。

首先不需要添加android:srcCompat来设置默认图像。

因为 app:srcCompat 用于向后兼容支持库的矢量绘图。

试试 android:src="@mipmap/ic_launcher"android:background="@mipmap/ic_launcher"

但是如果你想使用 srcCompat 属性,那么只需在 ImageView.

中将 android:srcCompat 更改为 app:srcCompat