轉載:http://blog.csdn.net/qq_28268507/article/details/70314844
自定義view繼承RadioButton
public class NotifyRadioButton extends RadioButton {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
float radius;
boolean notify;
public NotifyRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setColor(Color.RED);
radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 12.0f, context.getResources().getDisplayMetrics());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (notify) {
//擷取到DrawableTop, 0 drawableleft 1 drawabletop 2 drawableright 3 drawablebottom
Drawable drawable = getCompoundDrawables()[1];
//擷取到Drawable的left right top bottom的值
Rect bounds = drawable.getBounds();
//float cx, float cy, float radius, @NonNull Paint paint
float cx = getMeasuredWidth() / 2 + bounds.width() / 2 - radius / 2;
float cy = getPaddingTop() + bounds.height() / 4;
canvas.drawCircle(cx, cy, radius, paint);
}
}
/**
* 新消息提醒
*
* @param notify
*/
public void notify(boolean notify) {
this.notify = notify;
invalidate();
}
}
在代碼中,通過id找到控件,調用notify(true),就可看到小紅點的顯示