从Android Studio中的Java代码中读取样式项的值

huangapple go评论63阅读模式
英文:

read style item value from java code in android studio

问题

在Android Studio中,如果我有以下的style.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="Theme.MyTheme" parent="Theme.AppCompat">
            <item name="android:colorForeground">@color/purple_200</item>
        </style>
    </resources>

我要如何在Java中编写代码来读取“android:colorForeground”的十六进制值?
英文:

Say I have below style.xml file in android studio:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.MyTheme" parent="Theme.AppCompat">
        <item name="android:colorForeground">@color/purple_200</item>
    </style>
</resources>

How can I write code in java to read out the hex value of "android:colorForeground"?

答案1

得分: 1

val attr = intArrayOf(android.R.attr.colorForeground)
val typed = obtainStyledAttributes(R.style.{样式名称}, attr)
val foreCorlor = typed.getColorOrThrow(0)
typed.recycle() // 非常重要

英文:

do you want like this???

val attr = intArrayOf(android.R.attr.colorForeground)
val typed = obtainStyledAttributes(R.style.{the style name}, attr)
val foreCorlor = typed.getColorOrThrow(0)
typed.recyle() // very very important

huangapple
  • 本文由 发表于 2023年6月26日 09:52:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定