【问题标题】:How to change the background color of a button when being held in Android Studio?在 Android Studio 中按住时如何更改按钮的背景颜色?
【发布时间】:2021-12-19 21:24:52
【问题描述】:

因此,我在进行简单的代码修改时遇到了一些麻烦:我似乎无法做到,因此即使在使用颜色资源文件之后,按下按钮的颜色也会发生变化。当我这样做时,它只会更改背景并且图标会消失。

这是我的按钮代码:

<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="#1072E1"
        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" />

那么有没有一种简单的方法可以改变它的背景颜色呢?如您所见,背景颜色设置为#1072E1,但我想让它更具动态性并进行更改,因此当有人按住按钮时颜色会发生变化并在释放后恢复正常(按钮的颜色可能是#0E63C4 ,例如)。

这很简单,但我遇到了麻烦。如果有答案,我会提供更多详细信息。

【问题讨论】:

    标签: android background-color android-button


    【解决方案1】:

    您需要为按钮的按下状态设置不同的颜色,如SO answer所示

    【讨论】:

    • 好的,这个答案已经解决了,但我是否还需要 onCreate 方法,或者它是那个用户的私人事情?另外,我如何将这些不同的颜色应用于按钮“内部”的图标?我应该做同样的事情吗?
    • @André_Almeida onCreate 方法与按钮的颜色状态无关,如果您有我不知道的需要,您可以覆盖onCreate 方法。要将这些颜色应用于按钮,只需执行与答案相同的操作即可。
    • 我不想只改变按钮的背景颜色,我想要它的内部,因为还有一个图标。如果可能的话,我是否将一个颜色资源文件连接到 ImageButton?
    • 如果您想回复,我专门为它提出了一个新问题,提供更多详细信息:stackoverflow.com/questions/69882087/…
    • @André_Almeida 这不是您在这里提出的原始问题的答案吗?
    猜你喜欢
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    相关资源
    最近更新 更多