英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论