繁体   English   中英

在 Android Studio 中按住按钮本身时,如何更改按钮图标的颜色?

[英]How to change the color of a button's icon when the button itself is being held in Android Studio?

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

未举行

握住

如您所见,按钮变暗,但其中的图标不会变暗。

这是 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>

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

如果有一个更简单的解决方案,比如应用过滤器来改变整个事物的颜色,那么这会更容易。

// 将此添加到父级

xmlns:app="http://schemas.android.com/apk/res-auto"

// 然后将此添加到您的 ImageButton

应用程序:tint="@color/your_color"

这将改变 ImagButton 中的图标颜色

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM