public class TagView extends LinearLayout {

    GradientDrawable gradientDrawable;
    TextView textView;

    public SHTagView(Context context) {
        super(context);
        init();
    }

    public SHTagView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SHTagView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(Color.WHITE);
        gradientDrawable.setCornerRadius(30);
        gradientDrawable.setStroke(2, Color.parseColor("#C3C3C3"));
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        setBackgroundDrawable(gradientDrawable);

        int paddingLet = SHViewUtils.dp2px(getContext(), 15);
        int paddingTop = SHViewUtils.dp2px(getContext(), 5);
        setPadding(paddingLet, paddingTop, paddingLet, paddingTop);
        setGravity(Gravity.CENTER);

        textView = new TextView(getContext());
        addView(textView);
    }

    public String getText() {
        return textView.getText().toString();
    }

    public SHTagView setText(CharSequence text) {
        textView.setText(text);
        return this;
    }

    public SHTagView setTextColor(int color) {
        textView.setTextColor(color);
        return this;
    }

    public SHTagView setTextSize(float size) {
        textView.setTextSize(size);
        return this;
    }

    public SHTagView setBackgoundColor(int color) {
        gradientDrawable.setColor(color);
        return this;
    }

    public SHTagView setCornerRadius(int radius) {
        gradientDrawable.setCornerRadius(radius);
        return this;
    }

    public SHTagView setStroke(int width, int color) {
        gradientDrawable.setStroke(width, color);
        return this;
    }
}

标签: none

添加新评论