小编Арт*_*ния的帖子

如何在 Kotlin 中获取可绘制对象?

我正在 Android Studio 中开发一个小项目。我已将可绘制添加到 res/drawable 文件夹中。 在此输入图像描述

但是我无法从代码中获取它。我尝试过不同的方法:

ContextCompat.getDrawable(getActivity(), R.drawable.ic_training);
Run Code Online (Sandbox Code Playgroud)

val drawable: Drawable? = ContextCompat.getDrawable(context.applicationContext, R.drawable.ic_training )
val drawable2: Drawable = R.drawable.ic_training
Run Code Online (Sandbox Code Playgroud)

它们都返回错误未解决的引用。这是我的 XML 文件:

<vector
        android:id = "@+id/ic_training"
        android:alpha="0.85" android:height="24dp"
    android:viewportHeight="24.0" android:viewportWidth="24.0"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="ic_training"    >
    <path android:fillColor="#FF000000" android:pathData="M22,21c-1.11,0 -1.73,-0.37 -2.18,-0.64 -0.37,-0.22 -0.6,-0.36 -1.15,-0.36 -0.56,0 -0.78,0.13 -1.15,0.36 -0.46,0.27 -1.07,0.64 -2.18,0.64s-1.73,-0.37 -2.18,-0.64c-0.37,-0.22 -0.6,-0.36 -1.15,-0.36 -0.56,0 -0.78,0.13 -1.15,0.36 -0.46,0.27 -1.08,0.64 -2.19,0.64 -1.11,0 -1.73,-0.37 -2.18,-0.64 -0.37,-0.23 -0.6,-0.36 -1.15,-0.36s-0.78,0.13 -1.15,0.36c-0.46,0.27 -1.08,0.64 -2.19,0.64v-2c0.56,0 0.78,-0.13 1.15,-0.36 0.46,-0.27 1.08,-0.64 2.19,-0.64s1.73,0.37 2.18,0.64c0.37,0.23 0.59,0.36 1.15,0.36 0.56,0 0.78,-0.13 1.15,-0.36 …
Run Code Online (Sandbox Code Playgroud)

android drawable kotlin android-resources

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

android ×1

android-resources ×1

drawable ×1

kotlin ×1