我想在库中突出显示当前选定的图像。 有什么办法吗?
谢谢, 硝酸乙醇
答案 0 :(得分:1)
编辑:
您可以使用View类的mergeDrawableStates()方法来提供自定义状态:
http://code.google.com/android/reference/android/view/View.html#mergeDrawableStates(int[],%20int[])
尝试以下步骤创建新的自定义状态:
1)在res / values / attrs.xml中定义状态资源
<declare-styleable name="MyCustomState">
<attr name="state_fried" format="boolean" />
<attr name="state_baked" format="boolean" />
</declare-styleable>
2)在自定义视图类中:
A)声明状态变量:
private static final int[] FRIED_STATE_SET = {
R.attr.state_fried
};
private static final int[] BAKED_STATE_SET = {
R.attr.state_baked
};
B)重写onCreateDrawableState()方法:
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
if (isFried()) {
mergeDrawableStates(drawableState, FRIED_STATE_SET);
}
if (isBaked()) {
mergeDrawableStates(drawableState, BAKED_STATE_SET);
}
return drawableState;
}
完成此操作后,您应该能够在ColorStateListDrawable中使用它们,但您应该使用应用程序的命名空间来使用这些新状态:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/<my_app_package>">
<item android:drawable="@drawable/item_baked" state_baked="true"
state_fried="false" />
<item android:drawable="@drawable/item_fried" state_baked="false"
state_fried="true" />
<item android:drawable="@drawable/item_overcooked" state_baked="true"
state_fried="true" />
<item android:drawable="@drawable/item_raw" state_baked="false"
state_fried="false" />
</selector>