drawableのidentifierの値と、attrで指定した同じdrawableのidentifierを取りたかったんだけど、attrのidentifierとdrawableのidentifierは別物だったので、どうにかして、attrで指定しているdrawableのidentifierをとりたい!!ってことで調べたのでそのメモ。
答えは下記にあった。
cf: Access resource defined in theme and attrs.xml android
例えば以下の感じで指定しているstyles.xmlがあったとする。
<style name="OfficialAppTheme" parent="AppTheme">
<item name="hoge">@drawable/official_hoge</item>
</style>
<attr name="hoge" format="reference" /> こんなかんじでdrawableのidentifierはとれます
val identifier = context.resources.getIdentifier("official_hoge", "drawable", context.packageName) 上記と同じidentifierをattrからとるには、下記のようにひっぱてくる模様。
val styleIdentifier = context.resources.getIdentifier("OfficialAppTheme", "style", context.packageName)
val attrIdentifier = context.resources.getIdentifier("hoge", "attr", context.packageName)
val styleAttributes = context.theme.obtainStyledAttributes(styleIdentifier, intArrayOf(attrIdentifier))
val hogeIdentifier = styleAttributes.getResourceId(0, 0) ちょっと遠回りですがこんなかんじでとれますた。
こんにちは。virapture…
View Comments
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Your article helped me a lot, is there any more related content? Thanks!
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
Your article helped me a lot, is there any more related content? Thanks!