還在爲 textview以及button 的各類樣式而煩惱的童鞋們請往這裏看~~~~java
一次性解決 textview以及button的樣式,不再用寫xml了!!!android
所有動態預設置,拒絕堆代碼,拒絕xml...ide
支持的樣式以下字體
private int backColor = 0;//背景色 private int backColorSelected = 0;//按下後的背景色 private int backGroundImage = 0;//背景圖 private int backGroundImageSeleted = 0;//按下後的背景圖 private int textColor = Color.BLACK;//文字顏色 private int textColorSeleted = 0;//按下後的文字顏色 private float radius = 8;//圓角半徑 private int shape = 0;//圓角樣式,矩形、圓形等,因爲矩形的Id爲0,默認爲矩形 private Boolean fillet = false;//是否設置圓角 private int strokeWidth = 1;//邊框寬度 private int strokeColor = Color.TRANSPARENT;//邊框顏色
自定義屬性:this
<declare-styleable name="GCButton"> <attr name="backColors" format="color"/> <attr name="backColorSelected" format="color"/> <attr name="backGroundImage" format="reference"/> <attr name="backGroundImageSeleted" format="reference"/> <attr name="textColorSeleted" format="color"/> <attr name="radius" format="dimension"/> <attr name="shape"> <enum name="RECTANGLE" value="0"/> <enum name="OVAL" value="1"/> <enum name="LINE" value="2"/> <enum name="RING" value="3"/> </attr> <attr name="fillet" format="boolean"/> <attr name="strokeWidth" format="dimension"/> <attr name="strokeColor" format="color"/> </declare-styleable>
完整源碼:spa
import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.drawable.GradientDrawable; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.widget.Button; @SuppressLint("AppCompatCustomView") public class GCButton extends Button { private Context mContext; private GradientDrawable gradientDrawable; private int backColor = 0;//背景色 private int backColorSelected = 0;//按下後的背景色 private int backGroundImage = 0;//背景圖 private int backGroundImageSeleted = 0;//按下後的背景圖 private int textColor = Color.BLACK;//文字顏色 private int textColorSeleted = 0;//按下後的文字顏色 private float radius = 8;//圓角半徑 private int shape = 0;//圓角樣式,矩形、圓形等,因爲矩形的Id爲0,默認爲矩形 private Boolean fillet = false;//是否設置圓角 private int strokeWidth = 1;//邊框寬度 private int strokeColor = Color.TRANSPARENT;//邊框顏色 public GCButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.mContext = context; init(); initAttrs(attrs); } public GCButton(Context context, AttributeSet attrs) { this(context, attrs, 0); } public GCButton(Context context) { this(context, null); } private void initAttrs(AttributeSet attrs) { TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.GCButton); setStroke(a.getDimensionPixelSize(R.styleable.GCButton_strokeWidth, strokeWidth), a.getColor(R.styleable .GCButton_strokeColor, strokeColor)); setBackColor(a.getColor(R.styleable.GCButton_backColors, 0)); setBackColorSelected(a.getColor(R.styleable.GCButton_backColorSelected, 0)); setBackGroundImage(a.getInteger(R.styleable.GCButton_backGroundImage, 0)); setBackGroundImageSeleted(a.getInteger(R.styleable.GCButton_backGroundImageSeleted, 0)); setTextColorSelected(a.getColor(R.styleable.GCButton_textColorSeleted, 0)); setRadius(a.getDimensionPixelSize(R.styleable.GCButton_radius, 0)); setShape(a.getInt(R.styleable.GCButton_shape, 0)); setFillet(a.getBoolean(R.styleable.GCButton_fillet, false)); a.recycle(); TypedArray b = mContext.obtainStyledAttributes(attrs, new int[]{android.R.attr.textColor}); setTextColor(b.getColor(0, textColor)); b.recycle(); invalidateView(); } @Override public void setOnClickListener(@Nullable final OnClickListener l) { super.setOnClickListener(new NoDoubleClickListener() { @Override protected void onNoDoubleClick(View v) { l.onClick(v); } }); } private void init() { //將Button的默認背景色改成透明 // if (fillet) { // if (gradientDrawable == null) { // gradientDrawable = new GradientDrawable(); // } // gradientDrawable.setColor(Color.TRANSPARENT); // } else { // setBackgroundColor(Color.TRANSPARENT); // } //設置文字默認居中 setGravity(Gravity.CENTER); //設置Touch事件 setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent event) { //按下改變樣式 setColor(event.getAction()); //此處設置爲false,防止Click事件被屏蔽 return false; } }); setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } }); } //改變樣式 @SuppressWarnings("Range") private void setColor(int state) { if (state == MotionEvent.ACTION_DOWN) { //按下 if (backColorSelected != 0) { //先判斷是否設置了按下後的背景色int型 if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(backColorSelected); } else { setBackgroundColor(backColorSelected); } } else if (backColorSelected != 0) { if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(backColorSelected); } else { setBackgroundColor(backColorSelected); } } //判斷是否設置了按下後文字的顏色 if (textColorSeleted != 0) { setTextColor(textColorSeleted); } else if (backColorSelected != 0) { setTextColor(backColorSelected); } //判斷是否設置了按下後的背景圖 if (backGroundImageSeleted != 0) { setBackgroundResource(backGroundImageSeleted); } } if (state == MotionEvent.ACTION_UP) { //擡起 if (backColor == 0) { //若是沒有設置背景色,默認改成透明 if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(Color.TRANSPARENT); } else { setBackgroundColor(Color.TRANSPARENT); } } else if (backColor != 0) { if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(backColor); } else { setBackgroundColor(backColor); } } else { if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(backColor); } else { setBackgroundColor(backColor); } } //若是爲設置字體顏色,默認爲黑色 if (textColor == 0) { setTextColor(Color.BLACK); } else if (textColor != 0) { setTextColor(textColor); } else { setTextColor(textColor); } if (backGroundImage != 0) { setBackgroundResource(backGroundImage); } } } /** * 設置按鈕的背景色,若是未設置則默認爲透明 * * @param backColor */ public void setBackColor(int backColor) { this.backColor = backColor; } /** * 設置按鈕按下後的顏色 * * @param backColorSelected */ public void setBackColorSelected(int backColorSelected) { this.backColorSelected = backColorSelected; } /** * 設置按鈕的背景圖 * * @param backGroundImage */ public void setBackGroundImage(int backGroundImage) { this.backGroundImage = backGroundImage; } /** * 設置按鈕按下的背景圖 * * @param backGroundImageSeleted */ public void setBackGroundImageSeleted(int backGroundImageSeleted) { this.backGroundImageSeleted = backGroundImageSeleted; } /** * 設置按鈕圓角半徑大小 * * @param radius */ public void setRadius(float radius) { this.radius = radius; } /** * 設置按鈕文字顏色 * * @param textColor */ public void settextColor(int textColor) { this.textColor = textColor; setTextColor(textColor); } /** * 設置按鈕按下的文字顏色 * * @param textColor */ public void setTextColorSelected(int textColor) { this.textColorSeleted = textColor; } /** * 按鈕的形狀 * * @param shape */ public void setShape(int shape) { this.shape = shape; } /** * 設置其是否爲圓角 * * @param fillet */ @SuppressWarnings("deprecation") public void setFillet(Boolean fillet) { this.fillet = fillet; } public void setStroke(int strokeWidth, int strokeColor) { this.strokeWidth = strokeWidth; this.strokeColor = strokeColor; } public void invalidateView() { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } if (backColor == 0) { if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(Color.TRANSPARENT); } else { setBackgroundColor(Color.TRANSPARENT); } } else { if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(backColor); } else { setBackgroundColor(backColor); } } if (backGroundImage != 0) { setBackgroundResource(backGroundImage); } //GradientDrawable.RECTANGLE if (fillet) { gradientDrawable.setShape(shape); gradientDrawable.setCornerRadius(radius); gradientDrawable.setStroke(strokeWidth, strokeColor); setBackgroundDrawable(gradientDrawable); } } public abstract class NoDoubleClickListener implements OnClickListener { public static final int MIN_CLICK_DELAY_TIME = 1000; private long lastClickTime = 0; @Override public void onClick(View v) { long currentTime = System.currentTimeMillis(); if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) { lastClickTime = currentTime; onNoDoubleClick(v); } } protected abstract void onNoDoubleClick(View v); } }
須要設置邊框顏色,形狀等 fillet 屬性設置爲trueorm
後續會增長陰影等更多效果~~xml