2014-12-18 61 views
0

我有下面的选择器。它实际上是一个选择器内的形状。我这样做是因为显然Android 5不支持具有转角的形状,除非它在选择器内。以正常状态编程式更改选择器的颜色

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle"> 
      <corners 
       android:radius="@dimen/a_menu_corner_radius"/> 
     </shape> 
    </item> 
</selector> 

无论如何......事情是我想改变形状的颜色,以编程方式。我已经创建了这个实用方法:

/** 
* @param view 
* @param colorValue a color value, not a resource ID !!! 
* @throws java.lang.ClassCastException in case the drawable is not a selector 
*/ 
public static void setColorToSelectorNormalState(View view, int colorValue) throws ClassCastException { 
    if (view != null) { 
     Drawable background = view.getBackground(); 
     if (!(background instanceof StateListDrawable)) { 
      throw new ClassCastException("The drawable must inherit the StateListDrawable class. (the drawable set needs to be a selector !)"); 
     } 

     ((StateListDrawable) background).addState(new int[]{android.R.attr./*?????????*/}, new ColorDrawable(colorValue)); 
    } 
} 

......但我无法弄清楚作为一个状态参数要放什么。 (见/ /part)。无论如何,廷只有一个状态,即“正常”状态,但“正常”状态没有状态属性,或者是否存在?

任何想法?

回答

1

我想你需要的是StateSet.WILD_CARD

((StateListDrawable) background).addState(StateSet.WILD_CARD, new ColorDrawable(colorValue)); 

你可以找到的文档here

+0

试过了,也不行。不过谢谢。 – AndreiBogdan

+0

在设置新状态后,您还调用了refreshDrawableState吗? – Blackbelt

+0

不,但我只是试过了,它似乎没有工作。 – AndreiBogdan

相关问题