//获取Drawable图片资源的ID val drawableId=resources.getIdentifier("ic_launcher","drawable",packageName) val idDrawable=resources.getDrawable(drawableId) idDrawable.bounds.set(0,0,idDrawable.minimumWidth,idDrawable.minimumHeight) tvIdentifierDrawable.text=String.format(Locale.CHINA,"Drawable资源ID=%s",drawableId) tvIdentifierDrawable.setCompoundDrawables(null,null,null,idDrawable)
注:
tvIdentifierDrawable 是TextView的ID
获取的drawableID用于给TextView设置drarableBottom显示图片
2.2.3 根据mipmap图片名字获取资源id,设置显示图片
1 2 3 4 5 6
//获取mipmap图片资源的ID val mipmapId=resources.getIdentifier("ic_launcher_round","mipmap",packageName) val idMipmap=resources.getDrawable(mipmapId) idMipmap.bounds.set(0,0,idMipmap.minimumWidth,idMipmap.minimumHeight) tvIdentifierMipMap.text=String.format(Locale.CHINA,"Mipmap资源ID=%s",mipmapId) tvIdentifierMipMap.setCompoundDrawables(null,null,null,idMipmap)
注:
tvIdentifierMipMap 是TextView的ID
获取的mipmapId用于给TextView设置drarableBottom显示图片
2.2.4 根据R.string.xx名字资源id,设置字符串
1 2 3
//获取String字符串 val stringID=resources.getIdentifier("app_name","string",packageName) tvIdentifierString.text=String.format(Locale.CHINA,"String资源ID(%s)=%s",stringID,getString(stringID))
//获取Dimen大小,颜色等 val dimenID=resources.getIdentifier("sp_30","dimen",packageName) val colorID=resources.getIdentifier("colorAccent","color",packageName) val size=getString(dimenID) tvIdentifierDimen.apply { text=String.format(Locale.CHINA,"Dimen资源ID=%s,Color资ID=%s",dimenID,colorID) textSize=getString(dimenID).substring(0,getString(dimenID).indexOf("sp")).toFloat() setTextColor(resources.getColor(colorID)) }
注:
dimenID是R.dimen.sp_30通过getIdentifier获取的资源id
colorID是R.color.colorAccent通过getIdentifier获取的资源id
TextView.apply给textView中的文字大小和颜色赋值
2.2.6 根据R.style.xx名字获取资源id,设置文字样式
1 2 3 4
//获取style val styleID=resources.getIdentifier("MatchMatch","style",packageName) println(styleID) tvIdentifierStyle.setTextAppearance(this,styleID)