无法在Android中解析ContextCompat [英] Cannot Resolve ContextCompat in Android
问题描述
我有一个名为dialog的AlertDialogue对象。我正在尝试添加一个图标。我看到现在不推荐使用这种语法:
dialog.setIcon(getResources()。getDrawable(R.drawable.myImage);
我到处读到这应该有用:
dialog.setIcon(ContextCompat.getDrawable(context,R.drawable.myImage));
然而,Android Studio无法识别ContextCompat语法。我应该导入一些东西吗?谢谢。
***更新:感谢@Sharj下面的正确答案。如果你们需要视觉,我也制作了一个快速视频: https://www.youtube.com/watch?v=eFiaO0srQro&feature=youtu.be
ContextCompat 是支持的一部分library v4。你添加了支持库吗? 4你的项目?
android.support.v4.content.ContextCompat
如果你还没有
你可以在app文件夹下的build.gradle文件中包含支持库pre>
依赖项{
//其他东西在这里
编译'com.android.support:support-v4:23.0.0'
//更新23.0.0到最新版本
}
I have an AlertDialogue object called dialog. I am attempting to add an icon to it. I see that this syntax is now deprecated:
dialog.setIcon(getResources().getDrawable(R.drawable.myImage);
I'm reading everywhere that this should work:
dialog.setIcon(ContextCompat.getDrawable(context, R.drawable.myImage));
However, the ContextCompat syntax is not being recognized by Android Studio. Is there something that I should be importing? Thank you.
***Update: Thank's to @Sharj for the correct answer below. I made a quick video too if you guys need a visual: https://www.youtube.com/watch?v=eFiaO0srQro&feature=youtu.be
ContextCompat is part of support library v4. Have you added support library 4 to your project?
android.support.v4.content.ContextCompat
You can include support library to your build.gradle file under app folder if you haven't already
dependencies {
// other stuff here
compile 'com.android.support:support-v4:23.0.0'
// update the 23.0.0 to latest version available
}
这篇关于无法在Android中解析ContextCompat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!