【问题标题】:How to change text color programmatically in kotlin如何在 kotlin 中以编程方式更改文本颜色
【发布时间】:2023-03-19 23:15:01
【问题描述】:

我在 stakeOverFlow 上发现了类似的问题,但答案对我没有帮助

文件:values.xml

<color name= "orange_or">#EC7000</color>

我在 kotlin 中的 textView

private val myText: TextView bi bindView(R.id.my_text)

if (conditionTrue) {
    //my text color = orange_or
}

如何从 kotlin 中的 values.xml 文件中将此 textView 更改为该颜色?

【问题讨论】:

    标签: android xml kotlin


    【解决方案1】:

    你可以使用:

       //with a color  
       myText.setTextColor(ContextCompat.getColor(this,R.color.orange_or))
       //with a selector
       myText.setTextColor(AppCompatResources.getColorStateList(this, R.color.xxx))
    

    【讨论】:

    • 感谢您的回答,但我可以从 values.xml 中获取变量吗?
    • @ramossuit 这个变量是什么意思?您可以使用R.color.orange_or 获取值
    • 在项目值文件里面有我们在xml中使用的项目的所有颜色和值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多