自定义TextView的drawable可设置大小

方式一:代码设置

        TextView textView = findViewById(R.id.tv);
        Drawable drawable = getResources().getDrawable(R.drawable.apple);
        drawable.setBounds(0, 0, dp2px(45), dp2px(45));
        textView.setCompoundDrawables(drawable, null, null, null);//分别为上下左右位置的drawable

方式二:自定义TextView

在attrs文件中设置如下自定义属性

<declare-styleable name="TextViewDrawable">
        <attr name="drawable_width" format="dimension" />
        <attr name="drawable_height" format="dimension" />
    </declare-styleable>

public class TextViewDrawable extends android.support.v7.widget.AppCompatTextView {
    private static final int DIRECTION_WIDTH = 0;
    private static final int DIRECTION_HEIGHT = 1;
    private float drawableWidth;
    private float drawableHeight;
    private static final String TAG = TextViewDrawable.class.getSimpleName();

    public TextViewDrawable(Context context) {
        this(context, null);
    }

    public TextViewDrawable(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public TextViewDrawable(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextViewDrawable);
        drawableWidth = array.getDimension(R.styleable.TextViewDrawable_drawable_width, 0);
        drawableHeight = array.getDimension(R.styleable.TextViewDrawable_drawable_height, 0);
        array.recycle();
        if (drawableWidth > 0 && drawableHeight > 0) {
            Drawable[] drawables = getCompoundDrawables();
            setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]);
        }
    }

    @Override
    public void setCompoundDrawablesWithIntrinsicBounds(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
        if (left != null) {
            left.setBounds(0, 0, getSize(left, DIRECTION_WIDTH),
                    getSize(left, DIRECTION_HEIGHT));
        }
        if (right != null) {
            right.setBounds(0, 0, getSize(right, DIRECTION_WIDTH),
                    getSize(right, DIRECTION_HEIGHT));
        }
        if (top != null) {
            top.setBounds(0, 0, getSize(top, DIRECTION_WIDTH),
                    getSize(top, DIRECTION_HEIGHT));
        }
        if (bottom != null) {
            bottom.setBounds(0, 0, getSize(bottom, DIRECTION_WIDTH),
                    getSize(bottom, DIRECTION_HEIGHT));
        }
        setCompoundDrawables(left, top, right, bottom);
    }
    //获取图片的宽高
    private int getSize(Drawable drawable, int direction) {
        if (drawableWidth > 0 && drawableHeight > 0) {
            if (direction == DIRECTION_WIDTH) {
                return (int) drawableWidth;
            } else {
                return (int) drawableHeight;
            }
        } else {
            if (direction == DIRECTION_WIDTH) {
                return drawable.getIntrinsicWidth();
            } else {
                return drawable.getIntrinsicHeight();
            }
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值