自定义TextView:
- import com.ydk.mikecrm.R;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Paint.Style;
- import android.util.AttributeSet;
- import android.widget.TextView;
- /**
- * textView上的小红点
- * @author Visual
- *
- */
- public class RedTipTextView extends TextView {
- public static final int RED_TIP_INVISIBLE = 0;
- public static final int RED_TIP_VISIBLE = 1;
- public static final int RED_TIP_GONE = 2;
- private int tipVisibility = 0;
- public RedTipTextView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- init(null);
- }
- public RedTipTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- init(attrs);
- }
- public RedTipTextView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- // TODO Auto-generated constructor stub
- init(attrs);
- }
- public void init(AttributeSet attrs) {
- if(attrs != null) {
- TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.RedTipTextView);
- tipVisibility = array.getInt(R.styleable.RedTipTextView_redTipsVisibility, 0);
- array.recycle();
- }
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- if(tipVisibility == 1) {
- int width = getWidth();
- int paddingRight = getPaddingRight();
- Paint paint = new Paint();
- paint.setColor(Color.RED);
- paint.setAntiAlias(true);
- paint.setDither(true);
- paint.setStyle(Style.FILL_AND_STROKE);
- canvas.drawCircle(width - getPaddingRight() / 2, paddingRight / 2, paddingRight/2, paint);
- }
- }
- public void setVisibility(int visibility) {
- tipVisibility = visibility;
- invalidate();
- }
- }
attrs.xml添加
- <declare-styleable name="RedTipTextView">
- <attr name="redTipsVisibility">
- <enum name="invisible" value="0"></enum>
- <enum name="visible" value="1"></enum>
- <enum name="gone" value="2"></enum>
- </attr>
- </declare-styleable>
xml布局:
- android:id="@+id/sliding_menu_item_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:paddingRight="5dip"
- mike:redTipsVisibility="invisible"
- android:layout_toRightOf="@id/sliding_menu_item_icon"
- android:textColor="@color/text_light_gray_color"
- android:textSize="@dimen/sliding_menu_item_text_size"
-
顶
- 2
-
踩