2012-02-27 61 views
13

我有一个xml中定义背景的按钮。我想根据当前状态对按钮进行着色 - 即 - 按下,聚焦,正常。更改xml选择器中可绘制的色调

这是我的xml文件如下。另外,我的colored_tint_darkcolored_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> 

我知道有这个解决方案在Java中,但我专门找了一个XML格式的解决方案。谢谢。

回答

12

创建一个选择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> 

(在我的例子,选择和绿色未选中时,当图像是白色的)

然后在你的xml中,你可以添加色调属性到ImageView:

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

您可以在一个TextView使用文字颜色attibute也使用了这种选择:

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

您好,我有问题与阿比18,下[链接](http://stackoverflow.com/questions/38673196/crash色调选择-during-inflating-view-with-vector-drawable-tint-color-selector) 你有什么问题可以建议吗? – Alex 2016-08-02 16:53:16

+0

android:tint属性不适用于所有apis。要解决此问题,您可以直接使用正确的颜色创建.png文件,然后从ImageView中删除android:tint属性 – 2016-08-23 14:27:24

+0

我应该在哪里放置tint_menu_item.xml? – rraallvv 2017-02-05 16:00:48

相关问题