当按钮本身在Android Studio中被按住时,如何更改按钮图标的颜色? [英] How to change the color of a button's icon when the button itself is being held in Android Studio?

查看:0
本文介绍了当按钮本身在Android Studio中被按住时,如何更改按钮图标的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当按住按钮时,我在使按钮变暗时遇到了问题,经过一些帮助,它起作用了。但这只是按钮的背景,我想如果里面的图标也变暗会更好看。下面是一个例子:

Not held

Held

如您所见,按钮变深了,但里面的图标没有变深。

这是ImageButton的代码,它的src表示已经来自Android Studio的图片:

<ImageButton
        android:id="@+id/call_button"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginStart="248dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/my_button"
        android:contentDescription="@string/call"
        android:minWidth="48dp"
        android:src="@android:drawable/stat_sys_phone_call"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@id/iv_image"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.466" />

和只更改背景的";@Drawable/My_Button";的代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/dark_blue"/>
    <item android:state_focused="true" android:drawable="@color/dark_blue"/>
    <item android:drawable="@color/blue"/>
</selector>

是对图标所做的更改,因为从技术上讲,按钮是将被保持的按钮,而不是图标,这意味着在这样的两个文件之间可能会有连接,其中如果一个被保持,另一个也是(?)。

如果有一个更简单的解决方案,如应用滤镜来更改整个事物的颜色,那么它将变得容易得多。

推荐答案

将此添加到按钮的属性中,它有助于在单击项目或布局时显示覆盖背景色。

android:background="?android:attr/selectableItemBackground"

这篇关于当按钮本身在Android Studio中被按住时,如何更改按钮图标的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆