如何为ImageButton的图标添加强调颜色?

3

我正在跟随材料设计教程以及图标开发应用程序,但我发现一个应用程序可以在单击时更改图标颜色。我想做同样的事情。我该如何构建这样的功能?请帮助我解决这个问题,另外,在按钮单击时,snackbar的颜色也会改变。

enter image description here

谢谢你

2个回答

0
你可以使用 selector 来实现这个功能。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="resource when button is pressed"/>
    <item android:state_pressed="false" android:drawable="resource when button is not pressed"/>
</selector>

如果你想在你的按钮上应用这个选择器,按照以下步骤进行。

  1. 创建资源(.xml)文件。
  2. 在资源标签内输入xml代码(选择器)。
  3. 像下面这样设置按钮的背景:button.setbackground(@drawable/xml);

这是一个图像按钮,图标被设置为该图像按钮。那么如何在选择后更改图标的颜色? - AkhilGite
糟糕...我会尽快编辑答案。我写了关于<selector>标签的内容,但它没有出现。 - Kae10

0

尝试使用此选择器更改选择或图像按钮的图像

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_pressed = "true"
    android:drawable="@drawable/your_icon_pressed"/>
<item
    android:drawable="@drawable/your_icon"/>
</selector>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接