自定義TextView背景、邊框,不再使用樣式xml

項(xiàng)目寫了太多的樣式xml,做個這樣的可以不用再寫樣式xml了

public class ShapeTextView extends AppCompatTextView {
    /**
     * shapeBgSelectColor=選中背景色,shapeBgDefaultColor=未選中背景色,shapeStrokeSelectColor=選中邊框,
     * shapeStrokeDefaultColor=未選中邊框,shapeStrokeWidth=邊框?qū)挾?,shapeTextSelectColor=選中文字色,
     * shapeTextDefaultColor=未選中文字色
     * 當(dāng)用于靜態(tài)不需要改變UI狀態(tài)時(shí),應(yīng)當(dāng)使用*****DefaultColor
     */
    private int shapeBgSelectColor, shapeBgDefaultColor, shapeStrokeSelectColor,
            shapeStrokeDefaultColor, shapeStrokeWidth, shapeTextSelectColor,shapeTextDefaultColor;
    /**
     * shapeBgCorner=四角圓角大小,shapeBgCornerTopL=上左圓角大小,shapeBgCornerTopR=上右圓角大小,
     * shapeBgCornerBottomR=下右圓角大小,shapeBgCornerBottomL=下左圓角大小
     * 當(dāng)設(shè)置shapeBgCorner時(shí),其他單獨(dú)角設(shè)置無效,否則其他有效
     */
    private float shapeBgCorner, shapeBgCornerTopL, shapeBgCornerTopR, shapeBgCornerBottomR,
            shapeBgCornerBottomL;
    private ShapeTextView mTextView;

    public ShapeTextView(Context context) {
        super(context, null);
    }

    public ShapeTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mTextView = this;
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ShapeTextView);
        shapeBgSelectColor = attributes.getColor(R.styleable.ShapeTextView_shapeBgSelectColor, -1);
        shapeBgDefaultColor = attributes.getColor(R.styleable.ShapeTextView_shapeBgDefaultColor, -1);
        shapeBgCorner = attributes.getDimensionPixelSize(R.styleable.ShapeTextView_shapeBgCorner, -1);
        shapeBgCornerTopL = attributes.getDimensionPixelSize(R.styleable.ShapeTextView_shapeBgCornerTopL, 0);
        shapeBgCornerTopR = attributes.getDimensionPixelSize(R.styleable.ShapeTextView_shapeBgCornerTopR, 0);
        shapeBgCornerBottomR = attributes.getDimensionPixelSize(R.styleable.ShapeTextView_shapeBgCornerBottomR, 0);
        shapeBgCornerBottomL = attributes.getDimensionPixelSize(R.styleable.ShapeTextView_shapeBgCornerBottomL, 0);
        shapeStrokeSelectColor = attributes.getColor(R.styleable.ShapeTextView_shapeStrokeSelectColor, -1);
        shapeStrokeDefaultColor = attributes.getColor(R.styleable.ShapeTextView_shapeStrokeDefaultColor, -1);
        shapeStrokeWidth = attributes.getDimensionPixelSize(R.styleable.ShapeTextView_shapeStrokeWidth, -1);
        shapeTextSelectColor = attributes.getColor(R.styleable.ShapeTextView_shapeTextSelectColor, -1);
        shapeTextDefaultColor = attributes.getColor(R.styleable.ShapeTextView_shapeTextDefaultColor, -1);
        attributes.recycle();
        changeSelf();
    }

    public void changeSelf() {
        GradientDrawable drawable = new GradientDrawable();
        if (mTextView.isSelected()){
            if (shapeBgSelectColor != -1){
                drawable.setColor(shapeBgSelectColor);
            }
            if (shapeStrokeSelectColor != -1){
                drawable.setStroke(shapeStrokeWidth, shapeStrokeSelectColor);
            }
            mTextView.setTextColor(shapeTextSelectColor);
        } else {
            if (shapeBgDefaultColor != -1){
                drawable.setColor(shapeBgDefaultColor);
            }
            if (shapeStrokeDefaultColor != -1){
                drawable.setStroke(shapeStrokeWidth, shapeStrokeDefaultColor);
            }
            mTextView.setTextColor(shapeTextDefaultColor);
        }
        if (shapeBgCorner == -1){
            drawable.setCornerRadii(new float[]{shapeBgCornerTopL, shapeBgCornerTopL,
                    shapeBgCornerTopR, shapeBgCornerTopR, shapeBgCornerBottomR,
                    shapeBgCornerBottomR, shapeBgCornerBottomL, shapeBgCornerBottomL});
        } else {
            drawable.setCornerRadius(shapeBgCorner);
        }
        mTextView.setBackgroundDrawable(drawable);
    }

/**
     * 例如列表中不同狀態(tài)的標(biāo)簽
     * 用于改變背景及文字顏色
     * @param shapeBgDefaultColor 參數(shù)一 背景色
     * @param shapeTextDefaultColor 參數(shù)二 文本顏色
     */
    public void setShapeDefaultColor(int shapeBgDefaultColor, int shapeTextDefaultColor){
        this.shapeBgDefaultColor = shapeBgDefaultColor;
        this.shapeTextDefaultColor = shapeTextDefaultColor;
        changeSelf();
    }

    /**
     * 例如列表中不同狀態(tài)的標(biāo)簽
     * 用于改變背景、邊框色及文字顏色
     * @param shapeBgDefaultColor 參數(shù)一 背景色
     * @param shapeTextDefaultColor 參數(shù)二 文本顏色
     * @param shapeStrokeDefaultColor 參數(shù)三 邊框顏色顏色
     */
    public void setShapeDefaultColorStroke(int shapeBgDefaultColor, int shapeTextDefaultColor, int shapeStrokeDefaultColor){
        this.shapeBgDefaultColor = shapeBgDefaultColor;
        this.shapeTextDefaultColor = shapeTextDefaultColor;
        this.shapeStrokeDefaultColor = shapeStrokeDefaultColor;
        changeSelf();
    }
    
    private void use(){
        /*
          使用:xml:
               <com.ecovacs.manage.ui.ShapeTextView
                  android:id="@+id/tv1"
                  android:layout_width="120dp"
                  android:layout_height="40dp"
                  android:gravity="center"
                  android:text="自定義"
                  android:textSize="20dp"
                  app:shapeBgCorner="@dimen/dp_20"
                  app:shapeTextSelectColor="@color/white"
                  app:shapeBgSelectColor="@color/manage_blue"
                  app:shapeTextDefaultColor="@color/color_4d4d4d"
                  app:shapeBgDefaultColor="@color/color_ccc"/>
              activity:
               shapeTextView.setSelect(true/false);
               shapeTextView.changeSelf(); 
        */
    }
}

attrs.xml 添加屬性

<declare-styleable name="ShapeTextView">
        <attr name="shapeBgDefaultColor" format="color"/>
        <attr name="shapeBgSelectColor" format="color"/>
        <attr name="shapeTextDefaultColor" format="color"/>
        <attr name="shapeTextSelectColor" format="color"/>
        <attr name="shapeBgCorner" format="dimension"/>
        <attr name="shapeBgCornerTopL" format="dimension"/>
        <attr name="shapeBgCornerTopR" format="dimension"/>
        <attr name="shapeBgCornerBottomR" format="dimension"/>
        <attr name="shapeBgCornerBottomL" format="dimension"/>
        <attr name="shapeStrokeDefaultColor" format="color"/>
        <attr name="shapeStrokeSelectColor" format="color"/>
        <attr name="shapeStrokeWidth" format="dimension"/>
    </declare-styleable>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容