【发布时间】:2019-11-10 05:16:35
【问题描述】:
我正在关注kotlin fundamentals code lab,其中说API 21 下的任何内容都会将矢量图像转换为png 图像,并防止我需要将xmlns:app="http://schemas.android.com/apk/res-auto" 添加到activity_main.xml 文件并按Sync Now , 并将vectorDrawables.useSupportLibrary = true 添加到defaultConfig { } 文件中的defaultConfig { }) 文件中。我跟踪了所有内容,但一直收到错误消息:
/Users/Home/IdeaProjects/DiceRoller/app/src/main/res/layout/activity_main.xml:12
错误:找不到属性 android:srcCompat。
错误:链接文件资源失败。
我意识到xmlns:app="http://schemas.android.com/apk/res-auto" 行是灰色的
这是我正在使用的 gradle 版本:
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0-alpha18
是什么导致了这个问题?
activity_main.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
tools:context=".MainActivity">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/diceImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:srcCompat="@drawable/empty_dice"
tools:src="@drawable/dice_1">
</android.support.v7.widget.AppCompatImageView>
构建:等级(:应用程序)
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.diceroller"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.core:core-ktx:1.2.0-beta02'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}
build.gradle:
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0-alpha18'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
【问题讨论】:
-
顺便说一句,您提到的问题与您面临的真正问题无关,即在错误的命名空间中定义
srcCompat属性。 (灰色/灰色的命名空间是因为 Android Studio 识别出该命名空间未使用。)依赖关系也与问题无关。 -
(PS 提到您应该将
android:src替换为app:srcCompat的步骤甚至在该步骤的下方进一步提到:“更改<ImageView>元素中的android:src属性成为app:srcCompat。”) -
啊啊啊啊,我不知道。我刚开始学习 Kotlin。我以为这意味着它不可用。现在我知道这意味着它没有被使用,我永远不会忘记它。感谢您的信息:)