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

How can I get drawable in Kotlin?

我正在 Android Studio 做一个小项目。 我已将可绘制对象添加到 res/drawable 文件夹中。

但是我无法从代码中得到它。我尝试了不同的方法:

ContextCompat.getDrawable(getActivity(), R.drawable.ic_training);

val drawable: Drawable? = ContextCompat.getDrawable(context.applicationContext, R.drawable.ic_training )
val drawable2: Drawable = R.drawable.ic_training

它们都return 一个错误未解决的引用。 这是我的 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 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.22 0.6,0.36 1.15,0.36s0.78,-0.13 1.15,-0.36c0.45,-0.27 1.07,-0.64 2.18,-0.64s1.73,0.37 2.18,0.64c0.37,0.23 0.59,0.36 1.15,0.36v2zM22,16.5c-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.45,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.45,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.36s-0.78,0.13 -1.15,0.36c-0.47,0.27 -1.09,0.64 -2.2,0.64v-2c0.56,0 0.78,-0.13 1.15,-0.36 0.45,-0.27 1.07,-0.64 2.18,-0.64s1.73,0.37 2.18,0.64c0.37,0.22 0.6,0.36 1.15,0.36 0.56,0 0.78,-0.13 1.15,-0.36 0.45,-0.27 1.07,-0.64 2.18,-0.64s1.73,0.37 2.18,0.64c0.37,0.22 0.6,0.36 1.15,0.36s0.78,-0.13 1.15,-0.36c0.45,-0.27 1.07,-0.64 2.18,-0.64s1.73,0.37 2.18,0.64c0.37,0.22 0.6,0.36 1.15,0.36v2zM8.67,12c0.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.22 0.6,0.36 1.15,0.36s0.78,-0.13 1.15,-0.36c0.12,-0.07 0.26,-0.15 0.41,-0.23L10.48,5C8.93,3.45 7.5,2.99 5,3v2.5c1.82,-0.01 2.89,0.39 4,1.5l1,1 -3.25,3.25c0.31,0.12 0.56,0.27 0.77,0.39 0.37,0.23 0.59,0.36 1.15,0.36z"/>
    <path android:fillColor="#FF000000" android:pathData="M16.5,5.5m-2.5,0a2.5,2.5 0,1 1,5 0a2.5,2.5 0,1 1,-5 0"/>
</vector>

首先,检查根 drawable 中的 drawable,而不是 xxx 或其他东西

  • 也从矢量形状中删除 id

在你的 Activity 中做

val drawble = resources.getDrawable(R.drawable.ic_training,theme)

如果在片段中

val drawble = context!!.resources.getDrawable(R.drawable.ic_training,context!!.theme)

resources.getDrawable(R.drawable.ic_training, theme) 已弃用 使用这个

ContextCompat.getDrawable(getActivity(), R.drawable.name)

Android SDK 30

ContextCompat.getDrawable(this, R.drawable.name)

同样在 Kotlin 中你可以这样使用:(SDK 31)

val shape: Drawable? =
            ResourcesCompat.getDrawable(resources, R.drawable.custom_progress_bar, null)

其中 null : 没有主题属性的可绘制对象

但是,这个 link 显示 google 文档: https://developer.android.com/guide/topics/resources/drawable-resource#kotlin

这里没看到,这对我有用:

   var myShape = ContextCompat.getDrawable(
      applicationContext,
      R.drawable.shape_rectangle  //<-- name of xml file in res folder
   )