android 中的 ImageView 无法使用 srcCompat
Can't use srcCompat for ImageViews in android
我正在使用设计支持库 23.2。
我在我的 build.gradle 中添加了这些行,因为我的 Gradle 插件是版本 1.5
defaultConfig {
applicationId "com.abc.xyz"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
generatedDensities = []
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
在here
中指定
但是我不能为我的图像视图使用 srcCompat 属性。
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:srcCompat="@drawable/wallpaper"/>
其中@drawable/wallpaper为矢量资源文件
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M4,4h7V2H4c-1.1,0 -2,0.9 -2,2v7h2V4zm6,9l-4,5h12l-3,-4 -2.03,2.71L10,13zm7,-4.5c0,-0.83 -0.67,-1.5 -1.5,-1.5S14,7.67 14,8.5s0.67,1.5 1.5,1.5S17,9.33 17,8.5zM20,2h-7v2h7v7h2V4c0,-1.1 -0.9,-2 -2,-2zm0,18h-7v2h7c1.1,0 2,-0.9 2,-2v-7h-2v7zM4,13H2v7c0,1.1 0.9,2 2,2h7v-2H4v-7z"/>
它说
Error:(14) No resource identifier found for attribute 'srcCompat' in
package 'android'
我的 Gradle 版本是 1.5。如何使用 srcCompat?
调用 app:srcCompat
而不是 android:srcCompat
.
不要
android:srcCompat="@drawable/your_image"
DO
app:srcCompat="@drawable/your_image"
终于
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/wallpaper"/>
我的gradle版本是1.5
升级您的 Gradle 版本.
解决方案
Gradle Plugin 2.0+
com.android.tools.build:gradle:2.0.0-beta2
Vector drawables allow you to replace multiple png assets with a
single vector graphic, defined in XML. While previously limited to
Lollipop and higher devices, both VectorDrawable and
AnimatedVectorDrawable are now available through two new Support
Libraries support-vector-drawable and
support-animated-vector-drawable.
添加这个
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
您会注意到此新属性仅存在于 Gradle 插件的 2.0 版中。如果您使用的是 Gradle 1.5,您将改为使用
// Gradle Plugin 1.5
android {
defaultConfig {
generatedDensities = []
}
// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
将您的 gradle 插件更新到 2.0+
// Gradle Plugin 1.5
android {
defaultConfig {
generatedDensities = []
}
// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
additionalParameters "--no-version-vectors"
}
根据 google 开发者博客指南
http://android-developers.blogspot.in/2016/02/android-support-library-232.html
此库现在是 v7 AppCompat library
的依赖项,允许开发人员和 AppCompat 轻松使用矢量可绘制对象。
要在 ImageButton 或 ImageView 中使用 VectorDrawableCompat,使用 app:srcCompat XML 属性或 setImageResource() 方法。
要在 API 级别 20 或更低级别继续引用属性 ID,请将以下 appt 标志添加到您的构建中,gradle 文件:
如果您使用 Android 插件构建 Gradle 1.5.0 或更低版本,请将以下内容添加到您的 build.gradle 文件中:
android {
defaultConfig {
// Stops the Gradle’s automatic rasterization of vectors
generatedDensities = []
}
// Flag that tells aapt to keep the attribute ids
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
如果您使用 Android 插件构建 Gradle 2.0.0 或更高版本,请将以下内容添加到您的 build.gradle 文件中:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
不要
android:srcCompat="@drawable/wallpaper"
做
app:srcCompat="@drawable/wallpaper"
因为它 srcCompat 属性实际上是在 AppCompat 库中定义的。
重要您需要为此添加适当的命名空间。
xmlns:app="http://schemas.android.com/apk/res-auto"
重要
what you are getting it seems like it is just a lint error that can be
ignored. I have tried and have the same error, but it is working
correctly.
您可以使用 tools:ignore="MissingPrefix"
暂时避免看到此错误。
您正在使用 android:srcCompat
。它应该由 app:srcCompat
代替,因为它是在 AppCompat 中定义的属性,而不是在 android:
命名空间中。
第一个(在build.gradle)
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
第二个
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_traffic_black_24dp"
tools:ignore="MissingPrefix" />
第三个
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
我们开始了。
将多个答案合并为一个答案:
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/primary">
<android.support.v7.widget.AppCompatImageView
android:layout_width="300dp"
android:layout_height="100dp"
android:id="@+id/logoImageView"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:srcCompat="@drawable/my_logo_vector"
android:tint="@color/white"
/>
</RelativeLayout>
这对我有用。没有棉绒错误。使用 AppCompatImageView
.
我刚刚更改:
app:srcCompat="@drawable/wallpaper"
至
android:src="@drawable/wallpaper"
这对我有用。
您需要添加
xmlns:app="http://schemas.android.com/apk/res-auto"
到您的布局使其工作。在我添加它之前,我的 "app:srcCompat" 一直以红色突出显示。
有很多事情你必须关心
首先使用:
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
或
vectorDrawables {
useSupportLibrary = true
}
然后再使用
<android.support.v7.widget.AppCompatImageView
android:id="@+id/changeLanguages"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="@color/transparent"
android:scaleType="fitCenter"
app:srcCompat="@drawable/ic_transfer" />
始终使用 AppCompatImageView 或 Button,因为 Simple ImageView 不支持矢量图像
如果上述所有方法都不起作用,则使用
public class App extends Application {
@Override public void onCreate() {
super.onCreate();
// Make sure we use vector drawables
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
}
如果您正在使用 Activity,则使用 AppCompatActivty
扩展您的 Activity
public final class MainActivity extends AppCompatActivity {
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
确保您尝试使用的图片是 PNG 格式。
复制图片。
转到 android 层次结构中 1.Project TAB 上的 mipmap。
然后单击一次 mipmap(在 windows ctrl + v 上),当您为照片命名时,请确保其为小写字母且没有符号。
照片应该可以使用。
设置您的图像视图,(您可能需要暂时使用填充颜色)。
点击imageView,然后点击srcCompact(没有画笔的那一个,在@mipmap/"Your pictures name"上输入(这一步在属性选项卡中)
应该可以!
如果您需要内容描述,我刚刚使用了@String/StringPicture
我知道我迟到了,但它可能会对其他人有所帮助。即使我使用 app:SrcCompat,我也遇到了同样的问题。在花了很多时间之后,我认为这是由于可绘制对象造成的。我的问题只是通过将所有可绘制对象从可绘制对象 (v21) 移动到简单可绘制对象来解决。
我正在使用设计支持库 23.2。 我在我的 build.gradle 中添加了这些行,因为我的 Gradle 插件是版本 1.5
defaultConfig {
applicationId "com.abc.xyz"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
generatedDensities = []
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
在here
中指定但是我不能为我的图像视图使用 srcCompat 属性。
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:srcCompat="@drawable/wallpaper"/>
其中@drawable/wallpaper为矢量资源文件
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M4,4h7V2H4c-1.1,0 -2,0.9 -2,2v7h2V4zm6,9l-4,5h12l-3,-4 -2.03,2.71L10,13zm7,-4.5c0,-0.83 -0.67,-1.5 -1.5,-1.5S14,7.67 14,8.5s0.67,1.5 1.5,1.5S17,9.33 17,8.5zM20,2h-7v2h7v7h2V4c0,-1.1 -0.9,-2 -2,-2zm0,18h-7v2h7c1.1,0 2,-0.9 2,-2v-7h-2v7zM4,13H2v7c0,1.1 0.9,2 2,2h7v-2H4v-7z"/>
它说
Error:(14) No resource identifier found for attribute 'srcCompat' in package 'android'
我的 Gradle 版本是 1.5。如何使用 srcCompat?
调用 app:srcCompat
而不是 android:srcCompat
.
不要
android:srcCompat="@drawable/your_image"
DO
app:srcCompat="@drawable/your_image"
终于
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/wallpaper"/>
我的gradle版本是1.5
升级您的 Gradle 版本.
解决方案
Gradle Plugin 2.0+
com.android.tools.build:gradle:2.0.0-beta2
Vector drawables allow you to replace multiple png assets with a single vector graphic, defined in XML. While previously limited to Lollipop and higher devices, both VectorDrawable and AnimatedVectorDrawable are now available through two new Support Libraries support-vector-drawable and support-animated-vector-drawable.
添加这个
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
您会注意到此新属性仅存在于 Gradle 插件的 2.0 版中。如果您使用的是 Gradle 1.5,您将改为使用
// Gradle Plugin 1.5
android {
defaultConfig {
generatedDensities = []
}
// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
将您的 gradle 插件更新到 2.0+
// Gradle Plugin 1.5
android {
defaultConfig {
generatedDensities = []
}
// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
additionalParameters "--no-version-vectors"
}
根据 google 开发者博客指南
http://android-developers.blogspot.in/2016/02/android-support-library-232.html
此库现在是 v7 AppCompat library
的依赖项,允许开发人员和 AppCompat 轻松使用矢量可绘制对象。
要在 ImageButton 或 ImageView 中使用 VectorDrawableCompat,使用 app:srcCompat XML 属性或 setImageResource() 方法。
要在 API 级别 20 或更低级别继续引用属性 ID,请将以下 appt 标志添加到您的构建中,gradle 文件:
如果您使用 Android 插件构建 Gradle 1.5.0 或更低版本,请将以下内容添加到您的 build.gradle 文件中:
android {
defaultConfig {
// Stops the Gradle’s automatic rasterization of vectors
generatedDensities = []
}
// Flag that tells aapt to keep the attribute ids
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
如果您使用 Android 插件构建 Gradle 2.0.0 或更高版本,请将以下内容添加到您的 build.gradle 文件中:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
不要
android:srcCompat="@drawable/wallpaper"
做
app:srcCompat="@drawable/wallpaper"
因为它 srcCompat 属性实际上是在 AppCompat 库中定义的。
重要您需要为此添加适当的命名空间。
xmlns:app="http://schemas.android.com/apk/res-auto"
重要
what you are getting it seems like it is just a lint error that can be ignored. I have tried and have the same error, but it is working correctly.
您可以使用 tools:ignore="MissingPrefix"
暂时避免看到此错误。
您正在使用 android:srcCompat
。它应该由 app:srcCompat
代替,因为它是在 AppCompat 中定义的属性,而不是在 android:
命名空间中。
第一个(在build.gradle)
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
第二个
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_traffic_black_24dp"
tools:ignore="MissingPrefix" />
第三个
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
我们开始了。
将多个答案合并为一个答案:
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/primary">
<android.support.v7.widget.AppCompatImageView
android:layout_width="300dp"
android:layout_height="100dp"
android:id="@+id/logoImageView"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:srcCompat="@drawable/my_logo_vector"
android:tint="@color/white"
/>
</RelativeLayout>
这对我有用。没有棉绒错误。使用 AppCompatImageView
.
我刚刚更改:
app:srcCompat="@drawable/wallpaper"
至
android:src="@drawable/wallpaper"
这对我有用。
您需要添加
xmlns:app="http://schemas.android.com/apk/res-auto"
到您的布局使其工作。在我添加它之前,我的 "app:srcCompat" 一直以红色突出显示。
有很多事情你必须关心 首先使用:
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
或
vectorDrawables {
useSupportLibrary = true
}
然后再使用
<android.support.v7.widget.AppCompatImageView
android:id="@+id/changeLanguages"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="@color/transparent"
android:scaleType="fitCenter"
app:srcCompat="@drawable/ic_transfer" />
始终使用 AppCompatImageView 或 Button,因为 Simple ImageView 不支持矢量图像
如果上述所有方法都不起作用,则使用
public class App extends Application {
@Override public void onCreate() {
super.onCreate();
// Make sure we use vector drawables
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
}
如果您正在使用 Activity,则使用 AppCompatActivty
扩展您的 Activitypublic final class MainActivity extends AppCompatActivity {
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
确保您尝试使用的图片是 PNG 格式。
复制图片。
转到 android 层次结构中 1.Project TAB 上的 mipmap。
然后单击一次 mipmap(在 windows ctrl + v 上),当您为照片命名时,请确保其为小写字母且没有符号。
照片应该可以使用。
设置您的图像视图,(您可能需要暂时使用填充颜色)。
点击imageView,然后点击srcCompact(没有画笔的那一个,在@mipmap/"Your pictures name"上输入(这一步在属性选项卡中)
应该可以!
如果您需要内容描述,我刚刚使用了@String/StringPicture
我知道我迟到了,但它可能会对其他人有所帮助。即使我使用 app:SrcCompat,我也遇到了同样的问题。在花了很多时间之后,我认为这是由于可绘制对象造成的。我的问题只是通过将所有可绘制对象从可绘制对象 (v21) 移动到简单可绘制对象来解决。