可以用kotlin中的字符串获取颜色吗?

可以用kotlin中的字符串获取颜色吗?,kotlin,Kotlin,我看了一下这个问题,但在我输入这个问题的时候,没有任何关于Kotlin代码的答案 在我的colors.xml文件中,我如何访问这些颜色(例如使用字符串) <resources> <!-- Orange --> <color name="orangePrimary">#f6a02d</color> <color name="orange1">#e3952a</color

我看了一下这个问题,但在我输入这个问题的时候,没有任何关于Kotlin代码的答案

在我的
colors.xml
文件中,我如何访问这些颜色(例如使用字符串)

<resources>
    <!-- Orange -->
    <color name="orangePrimary">#f6a02d</color>
    <color name="orange1">#e3952a</color>
    <color name="orange2">#da8f28</color>
    <color name="orange3">#d08926</color>
</resources>
当android studio翻译代码时,我得到了这个

val desiredColour: Int = getResources().getColor(
                                    getResources().getIdentifier(
                                        "my_color",
                                        "color",
                                        getPackageName()
                                    )
但是
packageName()
getResources()
由于某种原因变成红色,这意味着出现了错误


那么Kotlin版本会是什么呢?

对此只有一种可能的解释。粘贴代码的地方不存在getPackageName()和getResources()

例如,如果我将您的代码粘贴到一个活动中,一切看起来都很好

val desiredColour = resources.getColor(resources.getIdentifier("my_color", "color", packageName))
主题:

val desiredColour = resources.getColor(resources.getIdentifier("my_color", "color", packageName),theme)
val desiredColour = resources.getColor(resources.getIdentifier("my_color", "color", activity?.packageName),activity?.theme)
object TestSingleObject {
    fun getDesiredColour(context: Context) =
        context.resources.getColor(context.resources.getIdentifier("my_color", "color", context.packageName), context.theme)
}
但如果我将其粘贴到片段中,我需要引用该片段的活动来获取包名

val desiredColour = resources.getColor(resources.getIdentifier("my_color", "color", activity?.packageName))
主题:

val desiredColour = resources.getColor(resources.getIdentifier("my_color", "color", packageName),theme)
val desiredColour = resources.getColor(resources.getIdentifier("my_color", "color", activity?.packageName),activity?.theme)
object TestSingleObject {
    fun getDesiredColour(context: Context) =
        context.resources.getColor(context.resources.getIdentifier("my_color", "color", context.packageName), context.theme)
}
如果出于某种原因,要将其粘贴到活动或片段之外的其他位置,则需要传递上下文或活动来调用这些方法

object TestSingleObject {
    fun getDesiredColour(context: Context) =
        context.resources.getColor(context.resources.getIdentifier("my_color", "color", context.packageName))
}
主题:

val desiredColour = resources.getColor(resources.getIdentifier("my_color", "color", packageName),theme)
val desiredColour = resources.getColor(resources.getIdentifier("my_color", "color", activity?.packageName),activity?.theme)
object TestSingleObject {
    fun getDesiredColour(context: Context) =
        context.resources.getColor(context.resources.getIdentifier("my_color", "color", context.packageName), context.theme)
}

您可以将粘贴Java代码复制到Kotlin文件中,IDE将自动为您将其转换为Kotlin。当我使用上述代码时,我得到以下翻译,其中有红色代码,表示错误或有许多不同库可供选择的代码,我不知道正确的翻译是什么。。val desiredColour:Int=getResources().getColor(getResources().getIdentifier(“我的颜色”,“颜色”,getPackageName()))我现在的另一个问题是getColor()已被弃用。@Jevon这不是问题。不要使用不推荐使用的方法。相反,通过提供主题来使用更新的getColor方法。提供一个主题应该不难。检查我的更新答案。当我更新版本时,我得到这个错误
调用需要API级别23(当前最小值为14):android.content.res.Resources#getColor
顺便说一句,我需要一个最小值14。@Jevon是的,带有主题的重载getColor方法是在很久以后引入的。要使用更新的方法,您需要更新您的min sdk版本,或者创建一个向后兼容的方法,以根据用户正在运行的sdk版本获取颜色。请改用
ResourcesCompat.getColor
)-有一组
ThingCompat
库帮助您的代码在一系列API级别上运行