【问题标题】:Android: how to change dynamically the button icon color?Android:如何动态更改按钮图标颜色?
【发布时间】:2015-08-30 17:57:49
【问题描述】:

我的布局中有这个按钮:

<Button
   android:id="@+id/followers_right"
   android:drawableLeft="@drawable/ic_team"
   android:text="@string/visibility_right_followers" />

默认情况下,我所有的图标都是灰色的。但在某些情况下,我想应用特定的颜色。对于可绘制按钮,我怎样才能以编程方式做到这一点?

myButton.setColorFilter(getResources().getColor(R.color.tint_selected));

此行不起作用(构建错误)。

感谢您的帮助!

【问题讨论】:

  • @hata 我认为他正在尝试从值中获取颜色。
  • 什么情况?这些案例是从服务器计算的还是从服务器接收的?这些情况是否取决于用户行为(按下、聚焦等)?
  • 不是背景颜色,而是图标颜色
  • 对不起,不是重复的。

标签: android button colors


【解决方案1】:

我试图自己为这个有趣的问题找到解决方案,但我还没有找到解决方案。一种解决方法是与Drawable 资源具有不同颜色的相同图标,

并在语法上更改按钮的图标,

yourButtom.setCompoundDrawablesWithIntrinsicBounds(R.drawable.low, 0, 0, 0); 

但唯一的问题是,如果你想要大量的颜色变化

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 2021-04-19
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多