【问题标题】:Kotlin easily access resources outside of an activityKotlin 轻松访问活动之外的资源
【发布时间】:2019-01-21 10:47:03
【问题描述】:

我是 Android 和 Kotlin 的新手。我正在尝试创建一个ResourcesHelper class,以便从我的应用程序中的任何其他自定义类轻松访问我的自定义颜色和字体。但是在这个助手中,我没有任何上下文。我已经阅读了获取扩展 Application 类的上下文的方法,但是编译器说我无法在 ResourcesHelper 伴随对象中访问此上下文,因为它会造成内存泄漏。我最后也得到了可选的链接。

这是我希望能够使用它的方式:

class ResourcesHelper {

    companion object {
        val lightBlue = resources.getColor(R.color.lightBlue)
        val customBlue = resources.getColor(R.color.customBlue)
        // [...]    
        val fontAwesome = resources.getFont(R.font.fontawesome)
        val lemonMilk = resources.getFont(R.font.lemonmilk)
    }
}

enum class ButtonStyle {
    MENU,
    // [...]
    VICTORY
}

class CustomButton(c: Context, attrs: AttributeSet) : Button(c, attrs) {

    var isButtonActivated = false

    fun setStyle(style: ButtonStyle) {
        setBackgroundColor(ResourcesHelper.transparent)

        when(style) {
            ButtonStyle.MENU -> {
                setText(R.string.menu_button)
                typeface = ResourcesHelper.lemonMilk
                setBackgroundColor(ResourcesHelper.customRed)
                setTextColor(ResourcesHelper.white)
            }
            // [...]
            ButtonStyle.VICTORY -> {
                setText(R.string.victory_button)
                typeface = ResourcesHelper.lemonMilk
                setBackgroundColor(ResourcesHelper.customRed)
                setTextColor(ResourcesHelper.white)
            }
        }
    }
}

我也阅读了这篇文章 Android access to resources outside of activity,但它是用 Java 编写的,我不知道如何在 Kotlin 中进行操作。

我完全不知道该怎么做以及如何做到这一点......或者是否有更好的方法来实现从任何地方获取资源。

感谢您的帮助

【问题讨论】:

  • 编译器说我无法在 ResourcesHelper 伴随对象中访问此上下文,因为它会造成内存泄漏 - 我认为这是一个警告,而不是错误。你也可以忽略它。应用上下文不能泄露
  • 谢谢!所以我得到一个可选的上下文。 val myCustomColor = context!!.getColor(R.color.lightBlue) 安全吗?我的 applicationContext 可以为空吗?
  • 我不明白你的意思。您使用应用程序类检索的上下文不可为空
  • 我尝试复制类似stackoverflow.com/questions/9445661/… 的内容,Android Studio 将其转换为 Kotlin 并使上下文可为空。但是我可以直接在我的 ResourcesHelper 中直接执行val context = Application().applicationContext,这样我就有了一个不可为空的上下文。
  • 虽然如果我喜欢这样做,它会使应用程序崩溃并显示Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109) at com...Common.ResourcesHelper.<clinit>(ResourcesHelper.kt:10)

标签: android kotlin


【解决方案1】:

对于颜色、字符串等系统资源可以使用Resources类,如下图

import android.content.res.Resources

    class ResourcesHelper {
    
        companion object {
            val lightBlue = Resources.getSystem().getColor(R.color.lightBlue)
    
        }
    
    }

【讨论】:

  • Resources.getColor(id) 现已弃用
【解决方案2】:

如果您想支持不同的 Android 版本,我建议您使用ContextCompat。它提供统一的接口来访问不同的资源并向后兼容旧的 Android 版本。

AnroidX 使用 androidx.core.content.ContextCompatSupportV4android.support.v4.content.ContextCompat

val lightBlue = ContextCompat.getColor(context, R.color.lightBlue)
val customBlue = ContextCompat.getColor(context, R.color.customBlue)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2011-01-21
    • 2021-11-20
    • 2011-09-24
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    相关资源
    最近更新 更多