【问题标题】:How to change the color of a button's icon when the button itself is being held in Android Studio?当按钮本身被保存在 Android Studio 中时,如何更改按钮图标的颜色?
【发布时间】:2021-12-21 05:04:12
【问题描述】:

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

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 android-button android-icons


    【解决方案1】:

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

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

    【讨论】:

    • 它使背景颜色“伪装”与我的应用程序的渐变背景。背景颜色已经设置好了,但如果图标与按钮相结合,它可能会起作用。问题是它们是两个独立的东西,正如您在上面的代码中看到的那样。 android:background 是颜色,android:src 是图像。我正在考虑制作一张图片。
    【解决方案2】:

    将此添加到您的 ImageButton

    app:tint="@color/your_color"
    

    【讨论】:

    • 默认情况下,这只是使按钮变暗。我只是希望在我按下它时发生这种情况。也许是主要活动中的颜色资源文件或方法?我只是还没有找到一个有效的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2021-12-19
    • 2023-02-21
    • 1970-01-01
    相关资源
    最近更新 更多