以编程方式更改 MaterialButton 图标的图标色调 [Kotlin]

leo*_*oLR 8 android kotlin android-vectordrawable

我需要更改 MaterialButton 的图标色调,该图标是一个 xml 矢量资产,我可以在 xml 布局中轻松更改色调,但是我需要通过单击以编程方式更改颜色,我无法找到与此问题相关的内容,这是我的按钮:

<com.google.android.material.button.MaterialButton
                android:id="@+id/btnShowDepartmentList"
                style="@style/com.madison.Button.IconButton"
                app:iconSize="32dp"
                android:padding="0dp"
                android:paddingLeft="5dp"
                android:paddingStart="5dp"
                app:icon="@drawable/ic_list_thumbnails"
                android:layout_width="42dp"
                android:layout_height="42dp"
                app:iconTint="@color/orangeLighter"
                tools:ignore="RtlSymmetry"/> ```
Run Code Online (Sandbox Code Playgroud)

Ben*_* P. 6

您将需要使用MaterialButton的setIconTint(ColorStateList)setIconTintResource(Int)方法。例如:

val button = findViewById<MaterialButton>(R.id.btnShowDepartmentList)
button.setOnClickListener {
    button.setIconTintResource(R.color.orangeLighter)
}
Run Code Online (Sandbox Code Playgroud)