简体   繁体   中英

How to change the background color of a button when being held in Android Studio?

So I'm having a little bit of trouble with an easy code modification: I can't seem to make it so a button's color changes after being held down, even after using a Color Resource File. When I do, it only changes the background and the icon disappears.

Here's my button's code:

<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" />

So is there a simple way to change the background color of it? As you can see the background color set to it is #1072E1, but I want to make it more dynamic and change it so when someone holds the button down the color changes and goes back to normal after it's released (button's color could be #0E63C4, for example).

It's so simple, yet I'm having trouble with it. I'll give further details if answers come in.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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