public FoodButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
private static final int[] STATE_FRIED = {R.attr.state_fried};
private static final int[] STATE_BAKED = {R.attr.state_baked};
private boolean mIsFried = false;
private boolean mIsBaked = false;
public void setFried(boolean isFried) {mIsFried = isFried;}
public void setBaked(boolean isBaked) {mIsBaked = isBaked;}
Then override function "onCreateDrawableState":
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
if (mIsFried) {
mergeDrawableStates(drawableState, STATE_FRIED);
}
if (mIsBaked) {
mergeDrawableStates(drawableState, STATE_BAKED);
}
return drawableState;
}
文件 "res/drawable/food_button.xml":
app:state_fried="false"
android:drawable="@drawable/item_baked" />
app:state_fried="true"
android:drawable="@drawable/item_fried" />
app:state_fried="true"
android:drawable="@drawable/item_overcooked" />
app:state_fried="false"
android:drawable="@drawable/item_raw" />