ImageTextButton(图文按键)

121 阅读1分钟

用来解决开发中经常遇到的文字+图片展示的TextView, 因原生TextView的drawableTop等会受到UI切图的局限,所以对TextView进行了二次封装,使它可以自由的控制图片的大小。

public class ImageTextButton extends TextView {

    private Drawable drawableLeft;
    private Drawable drawableTop;
    private Drawable drawableRight;
    private Drawable drawableBottom;

    private int drawablePadding;
    private int drawableWidth;
    private int drawableHeight;

    public ImageTextButton(Context context) {
        super(context);
        init(context, null);
    }

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

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

    private void init(Context context, AttributeSet attrs) {
        if (null != attrs) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageTextButton);
            drawablePadding = typedArray.getDimensionPixelSize(R.styleable.ImageTextButton_drawablePadding, 0);
            drawableWidth = typedArray.getDimensionPixelSize(R.styleable.ImageTextButton_drawableWidth, 0);
            drawableHeight = typedArray.getDimensionPixelSize(R.styleable.ImageTextButton_drawableHeight, 0);
            typedArray.recycle();
            drawableLeft = null == getCompoundDrawablesRelative()[0] ? getCompoundDrawables()[0] : getCompoundDrawablesRelative()[0];
            drawableTop = null == getCompoundDrawablesRelative()[1] ? getCompoundDrawables()[1] : getCompoundDrawablesRelative()[1];
            drawableRight = null == getCompoundDrawablesRelative()[2] ? getCompoundDrawables()[2] : getCompoundDrawablesRelative()[2];
            drawableBottom = null == getCompoundDrawablesRelative()[3] ? getCompoundDrawables()[3] : getCompoundDrawablesRelative()[3];
            initView();
        }
    }

    public void setDrawableTop(int resId) {
        this.drawableTop = ContextCompat.getDrawable(getContext(), resId);
        initView();
    }

    public void setDrawableTop(Drawable drawable) {
        this.drawableTop = drawable;
        initView();
    }

    public void setDrawableBottom(int resId) {
        this.drawableBottom = ContextCompat.getDrawable(getContext(), resId);
        initView();
    }

    public void setDrawableLeft(int resId) {
        this.drawableLeft = ContextCompat.getDrawable(getContext(), resId);
        initView();
    }

    public void setDrawableRight(int resId) {
        this.drawableRight = ContextCompat.getDrawable(getContext(), resId);
        initView();
    }

    public void setDrawableWidth(int drawableWidth) {
        this.drawableWidth = drawableWidth;
        initView();
    }

    public void setDrawableHeight(int drawableHeight) {
        this.drawableHeight = drawableHeight;
        initView();
    }

    public void setDrawablePadding(int drawablePadding) {
        this.drawablePadding = drawablePadding;
        initView();
    }

    public void initView(int drawableWidth, int drawableHeight, int drawablePadding) {
        this.drawableWidth = drawableWidth;
        this.drawableHeight = drawableHeight;
        this.drawablePadding = drawablePadding;
        initView();
    }

    public void initView(int drawableWidth, int drawableHeight, int drawablePadding, Drawable drawableLeft, Drawable drawableRight, Drawable drawableTop, Drawable drawableBottom) {
        this.drawableWidth = drawableWidth;
        this.drawableHeight = drawableHeight;
        this.drawablePadding = drawablePadding;

        this.drawableLeft = drawableLeft;
        this.drawableRight = drawableRight;
        this.drawableTop = drawableTop;
        this.drawableBottom = drawableBottom;

        initView();
    }

    /**
     * 动态设置 TextView 样式
     */
    public void setTextAppearance(int styleId) {
        setTextAppearance(getContext(), styleId);
    }

    @Override
    public void setTextSize(float size) {
        super.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }

    private void initView() {
        if (null != drawableLeft) {
            drawableLeft.setBounds(0, 0, drawableWidth, drawableHeight);
            setCompoundDrawablePadding(drawablePadding);
        }

        if (null != drawableTop) {
            drawableTop.setBounds(0, 0, drawableWidth, drawableHeight);
            setCompoundDrawablePadding(drawablePadding);
        }

        if (null != drawableRight) {
            drawableRight.setBounds(0, 0, drawableWidth, drawableHeight);
            setCompoundDrawablePadding(drawablePadding);
        }

        if (null != drawableBottom) {
            drawableBottom.setBounds(0, 0, drawableWidth, drawableHeight);
            setCompoundDrawablePadding(drawablePadding);
        }
        setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
    }
}

attrs.xml

<declare-styleable name="ImageTextButton">
    <attr name="drawableWidth" format="dimension" />
    <attr name="drawableHeight" format="dimension" />
    <attr name="drawablePadding" format="dimension" />
</declare-styleable>

使用方式:

<com.cn.ImageTextButton
    android:id="@+id/itb_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:drawableTop="@drawable/ic_start_meeting"
    android:gravity="center"
    android:text="发起会议"
    android:textColor="@color/color_333333"
    android:textSize="12sp"
    app:drawableHeight="50dp"
    app:drawablePadding="10dp"
    app:drawableWidth="50dp"/>