【问题标题】:How can I get drawable in Kotlin?如何在 Kotlin 中绘制可绘制对象?
【发布时间】:2020-04-28 18:08:49
【问题描述】:

我正在 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

它们都返回错误未解析的引用。 这是我的 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文件夹中还是在其他文件夹中放错了位置?
  • 检查它是否在drawable而不是drawable-v24和任何其他带有任何修饰符的文件夹中
  • drawable 以它所在的文件命名,而不是 android:id 元素。
  • edit您的问题提供完整的错误信息。错误是仅在编辑器中显示还是在构建项目时出现错误?

标签: android kotlin drawable android-resources


【解决方案1】:

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

  • 也从 Vector 形状中删除 id

在你的活动中做

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

如果在片段中

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

【讨论】:

  • 未解决的参考:资源
【解决方案2】:

Android SDK 30

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      Kotlin 中您也可以这样使用:(SDK 31)

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

      where null : 没有主题属性的drawables

      然而,这个链接是关于它的显示谷歌文档: https://developer.android.com/guide/topics/resources/drawable-resource#kotlin

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-27
        • 1970-01-01
        • 1970-01-01
        • 2013-08-28
        • 2016-11-04
        • 1970-01-01
        • 2014-10-20
        相关资源
        最近更新 更多