如何在选择器的 drawable 中应用色调?

Vin*_*hod 3 layout android image shape selector

我有 2 张图像 ( normal, pressed),我想在按钮的选择器中设置

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
        android:drawable="@drawable/recent_pressed" />
    <item
         android:drawable="@drawable/recent" />
</selector>
Run Code Online (Sandbox Code Playgroud)

现在我想在@drawable/recent_pressed图像中应用色调颜色。

可以对此有任何解决方案。

我不想为 imageview 创建自定义类,因为此选择器用作菜单。

我搜索了这个,但这个链接对我不起作用

小智 9

创建一个选择器 tint_tem.xml :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_pressed="true" />
<item android:color="@color/white" android:state_activated="true" />
<item android:color="@color/green" />
Run Code Online (Sandbox Code Playgroud)

然后在您的 xml 中,您可以向 ImageView 添加 tint 属性:

<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:tint="@color/tint_item"
android:src="@drawable/ic_menu_home" />
Run Code Online (Sandbox Code Playgroud)

您还可以使用 textColor 属性在 TextView 上使用此选择器:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/tint_item" />
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以创建位图并在其上应用色调。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <bitmap android:src="@drawable/your_pressed_drawable" android:tint="@color/colorPrimary"/>
    </item>
    <item android:drawable="@drawable/your_second_drawable" />
</selector>
Run Code Online (Sandbox Code Playgroud)

  • 是的,但前提是可绘制对象是位图,而不是其他可绘制对象,例如矢量可绘制对象 (9认同)