StateListDrawable
포함 된 드로어 블에 적용된 색상 필터를 무시 하는 것 같습니다 . 예를 들면 :
StateListDrawable sld = new StateListDrawable();
Drawable pressedState = Context.getResources().getDrawable(R.drawable.solid_green);
pressedState.setColorFilter(Color.RED, PorterDuff.Mode.SRC);
sld.addState(new int[] {android.R.attr.state_pressed}, pressedState);
// Other states...
sld
뷰의 배경에 적용 하면 뷰의 배경을 눌렀을 때 빨간색으로 바뀔 것으로 예상 할 수 있습니다. 대신 pressedState
필터가 적용되지 않은 의 색상 인 녹색으로 바뀝니다 .
이 문제를 해결하려면 StateListDrawable
드로어 블이있는 상태에 따라 색상 필터를 자체 에 적용해야합니다 . 다음 확장은 StateListDrawable
이 를 수행합니다.
public class SelectorDrawable extends StateListDrawable {
public SelectorDrawable(Context c) {
super();
addState(new int[] {android.R.attr.state_pressed}, c.getResources().getDrawable(R.drawable.solid_green));
// Other states...
}
@Override
protected boolean onStateChange(int[] states) {
boolean isClicked = false;
for (int state : states) {
if (state == android.R.attr.state_pressed) {
isClicked = true;
}
}
if (isClicked)
setColorFilter(Color.RED, PorterDuff.Mode.SRC);
else
clearColorFilter();
return super.onStateChange(states);
}
}
로직 입력 onStateChange(int[] states)
은 눌려진 상태 이상을 테스트하기 위해 더 확장 될 수 있으며 그에 따라 다른 색상 필터를 적용 할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다