라이브러리 프로젝트에 대한 내 사용자 정의보기의 배경으로 프로그래밍 방식으로 StateListDrawable
을 설정하려고합니다.StateListDrawable이 작동하지 않습니다.
final TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.ActionBar);
int firstColor = a.getColor(
R.styleable.ActionBar_backgroundGradientFirstColor, 0xff000000);
int secondColor = a
.getColor(R.styleable.ActionBar_backgroundGradientSecondColor,
0xff000000);
int textViewColor = a.getColor(R.styleable.ActionBar_titleColor,
0xffffffff);
int onClickColor = a.getColor(
R.styleable.ActionBar_backgroundClickedColor, 0xff999999);
a.recycle();
StateListDrawable sld = new StateListDrawable();
GradientDrawable drawable = new GradientDrawable(
Orientation.TOP_BOTTOM, new int[] { firstColor, secondColor });
sld.addState(new int[] { android.R.attr.state_enabled },
new ColorDrawable(onClickColor));
sld.addState(new int[] { android.R.attr.state_pressed }, drawable);
action2.setBackgroundDrawable(sld);
action3.setBackgroundDrawable(sld);
actionBack.setBackgroundDrawable(sld);
pb.setBackgroundDrawable(drawable);
tv.setBackgroundDrawable(drawable);
tv.setTextColor(textViewColor);
그러나,이 작동하지 않습니다 : 여기에 내가 뭘 무엇 그것은 항상 가 상태를 활성화립니다. 그려야 만 상태 일 때 버튼을 누릅니다. 내가 도대체 뭘 잘못하고있는 겁니까?
:이 가능한 사용자 정의되고 싶어하기 때문에 XML을 사용하는 것은 옵션이 아니다는 (그것은 UI 라이브러리, 그래서는 사용자가 XML을 통해 그것을 사용자 정의 할 수 있도록하려면) – razielsarafan