老的方法都是FrameLayout加一个foreground,这里是用自定义view去实现
public class PressedTextView extends AppCompatTextView {
public PressedTextView(Context context) {
super(context);
}
public PressedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PressedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (isPressed()) {
canvas.drawColor(0x33000000);
}
}
@Override
protected void dispatchSetPressed(boolean pressed) {
super.dispatchSetPressed(pressed);
invalidate();
}
}
网友评论