自定义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文件中设置如下自定义属性


       
       
   

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();
            }
        }
    }
}
 

你可能感兴趣的:(TextView)