這是從我的應用程序:
public static Drawable getButtonDrawableByScreenCathegory(Context con,
ScreenCategory screenCategory, ButtonType buttonType) {
int normalStateResID = (int) GXConstants.NOT_ID;
int pressedStateResID = R.drawable.button_header_pressed;
switch (screenCategory) {
...
}
Drawable state_normal = con.getResources()
.getDrawable(normalStateResID).mutate();
Drawable state_pressed = con.getResources()
.getDrawable(pressedStateResID).mutate();
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[] { android.R.attr.state_pressed },
state_pressed);
drawable.addState(new int[] { android.R.attr.state_enabled },
state_normal);
return drawable;
}
要設置背景,比如說,按鈕,我呼籲:
button.setBackgroundDrawable(GXUtils.getButtonDrawableByScreenCathegory(
this, mScreenCategory, ButtonType.MENU)