美文网首页
自定义TextView背景、边框,不再使用样式xml

自定义TextView背景、边框,不再使用样式xml

作者: 带带我 | 来源:发表于2022-05-20 15:14 被阅读0次

项目写了太多的样式xml,做个这样的可以不用再写样式xml了

public class ShapeTextView extends AppCompatTextView {
    /**
     * shapeBgSelectColor=选中背景色,shapeBgDefaultColor=未选中背景色,shapeStrokeSelectColor=选中边框,
     * shapeStrokeDefaultColor=未选中边框,shapeStrokeWidth=边框宽度,shapeTextSelectColor=选中文字色,
     * shapeTextDefaultColor=未选中文字色
     * 当用于静态不需要改变UI状态时,应当使用*****DefaultColor
     */
    private int shapeBgSelectColor, shapeBgDefaultColor, shapeStrokeSelectColor,
            shapeStrokeDefaultColor, shapeStrokeWidth, shapeTextSelectColor,shapeTextDefaultColor;
    /**
     * shapeBgCorner=四角圆角大小,shapeBgCornerTopL=上左圆角大小,shapeBgCornerTopR=上右圆角大小,
     * shapeBgCornerBottomR=下右圆角大小,shapeBgCornerBottomL=下左圆角大小
     * 当设置shapeBgCorner时,其他单独角设置无效,否则其他有效
     */
    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);
    }

/**
     * 例如列表中不同状态的标签
     * 用于改变背景及文字颜色
     * @param shapeBgDefaultColor 参数一 背景色
     * @param shapeTextDefaultColor 参数二 文本颜色
     */
    public void setShapeDefaultColor(int shapeBgDefaultColor, int shapeTextDefaultColor){
        this.shapeBgDefaultColor = shapeBgDefaultColor;
        this.shapeTextDefaultColor = shapeTextDefaultColor;
        changeSelf();
    }

    /**
     * 例如列表中不同状态的标签
     * 用于改变背景、边框色及文字颜色
     * @param shapeBgDefaultColor 参数一 背景色
     * @param shapeTextDefaultColor 参数二 文本颜色
     * @param shapeStrokeDefaultColor 参数三 边框颜色颜色
     */
    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>

相关文章

网友评论

      本文标题:自定义TextView背景、边框,不再使用样式xml

      本文链接:https://www.haomeiwen.com/subject/gsstprtx.html