简体   繁体   English

更改 xml 选择器中 drawable 的色调

[英]Change the tint of drawable in xml selector

I have a button with the background defined in xml.我有一个在 xml 中定义背景的按钮。 I would like to tint the button based on the current state it is in - ie - pressed, focussed, normal.我想根据按钮的当前状态为按钮着色 - 即 - 按下、聚焦、正常。

Here is my xml file below.下面是我的 xml 文件。 Also, my colored_tint_dark , and colored_tint are both translucent colors that I am trying to draw over the drawable image that I call from the resources folder.此外,我的colored_tint_darkcolored_tint都是半透明的颜色,我试图在从资源文件夹调用的可绘制图像上绘制它们。 Here is the problem.这是问题所在。 When the UI first loads, the image has the appropriate tint on it, but after pressed, the pressed state doesn't show any tint, then the normal state won't show any tint.当用户界面第一次加载时,图像上有适当的色调,但按下后,按下状态不显示任何色调,然后正常状态不会显示任何色调。

<?xml version="1.0" encoding="utf-8"?>

<item android:state_pressed="true" android:drawable="@drawable/rounded_grayscale_pinstripe_button">
    <shape>
        <gradient
            android:endColor="@color/colored_tint"
            android:startColor="@color/colored_tint"
            android:angle="270" />
        <stroke
            android:width="0dp"
            android:color="@color/colored_tint" />
        <corners
            android:radius="0dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<item android:state_focused="true" android:drawable="@drawable/rounded_grayscale_pinstripe_button">
    <shape>
        <gradient
            android:endColor="@color/colored_tint"
            android:startColor="@color/colored_tint"
            android:angle="270" />
        <stroke
            android:width="0dp"
            android:color="@color/colored_tint" />
        <corners
            android:radius="0dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<item android:drawable="@drawable/rounded_grayscale_pinstripe_button">        
    <shape>
        <gradient
            android:endColor="@color/colored_tint_dark"
            android:startColor="@color/colored_tint_dark"
            android:angle="270" />
        <stroke
            android:width="0dp"
            android:color="@color/colored_tint_dark" />
        <corners
            android:radius="0dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

I know that there are solutions to this in java, but I am specifically looking for a solution in xml.我知道在 java 中有解决方案,但我专门在 xml 中寻找解决方案。 Thanks.谢谢。

Create a selector tint_menu_item.xml :创建一个选择器 tint_menu_item.xml :

<?xml version="1.0" encoding="utf-8"?>
<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" />
</selector>

(In my example, image is white when selected, and green when not selected) (在我的例子中,选择时图像为白色,未选择时为绿色)

Then in your xml, you can add tint attribute to ImageView:然后在您的 xml 中,您可以向 ImageView 添加 tint 属性:

<ImageView
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:tint="@color/tint_menu_item"
    android:src="@drawable/ic_menu_home" />

You can also use this selector on a TextView using textColor attibute:您还可以使用 textColor 属性在 TextView 上使用此选择器:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/tint_menu_item" />

Have you tried with a selector ?您是否尝试过使用选择器

You can find some examples HERE你可以在这里找到一些例子

let's say you have an appCpmpatImageView and add this **app**:tint="@drawable/custom_tint" to the compat image view and your custom tint selector is like this:假设您有一个appCpmpatImageView并将这个**app**:tint="@drawable/custom_tint"到兼容图像视图中,您的自定义色调选择器如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/grey" android:state_pressed="true" />
    <item android:color="@color/silver" android:state_focused="true" />
    <!-- default tint -->
    <item android:color="@color/white" />
</selector>

so your appCompat ImageView will use each color as a tint in different situations.因此您的 appCompat ImageView 将在不同情况下使用每种颜色作为色调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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