[英]How to programmaticaly change color of a color resource?
我想更改一个带有颜色集的形状的按钮:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/custom"/>
<corners android:bottomLeftRadius="10dp"/>
</shape>
在我的颜色列表资源中,我有<color name="custom">#A281E0</color>
。 是否可以以编程方式更改自定义的颜色? 提前致谢
也许使用动态颜色更改会更好,如下所示:
String color = "your hex color"
Int colorToUse = Color.parseColor(color)
然后设置按钮的背景颜色:
Int buttonId = findViewById(R.id.button_id)
buttonId.setBackgroundColor(colorToUse)
设置您需要为变量color
。 像这样的东西。
我无法发表评论,因为我没有足够的声誉。
为什么需要这样做? 也许还有另一种攻击计划来解决需求,例如状态列表可绘制,它将允许基于不同状态的不同可绘制 - 即选择、聚焦等。
https://developer.android.com/guide/topics/resources/drawable-resource#StateList
因为我是新手,我不能将其标记为重复,但我认为这个问题会给你你想要的答案。 显然,您可以创建一个扩展资源的类并覆盖方法 getColor(int)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.