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)。無論如何,廷只有一個狀態,即「正常」狀態,但「正常」狀態沒有狀態屬性,或者是否存在?
任何想法?
試過了,也不行。不過謝謝。 – AndreiBogdan
在設置新狀態後,您還調用了refreshDrawableState嗎? – Blackbelt
不,但我只是試過了,它似乎沒有工作。 – AndreiBogdan