簡體   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