Android 设置TextView Drawable大小的几种方法 发表于 2020-04-10 | 第一种 使用 layer-list1234567891011<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"><!--设置图片的大小 --> <item android:width="45dp" android:height="45dp"> <bitmap android:src="@drawable/icon_profit" /> </item></layer-list> 第二种 使用自定义控件123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128/** * 可自定义设置drawable宽高的TextView */public class DrawableTextView extends AppCompatTextView { private Drawable drawableLeft; private Drawable drawableRight; private Drawable drawableTop; private int leftWidth; private int rightWidth; private int topWidth; private int leftHeight; private int rightHeight; private int topHeight; private Context mContext; public DrawableTextView(Context context) { super(context); this.mContext = context; init(context, null); } public DrawableTextView(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; init(context, attrs); } public DrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mContext = context; init(context, attrs); } private void init(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView); drawableLeft = typedArray.getDrawable(R.styleable.DrawableTextView_leftDrawable); drawableRight = typedArray.getDrawable(R.styleable.DrawableTextView_rightDrawable); drawableTop = typedArray.getDrawable(R.styleable.DrawableTextView_topDrawable); if (drawableLeft != null) { leftWidth = typedArray.getDimensionPixelOffset(R.styleable.DrawableTextView_leftDrawableWidth, dip2px(context, 20)); leftHeight = typedArray.getDimensionPixelOffset(R.styleable.DrawableTextView_leftDrawableHeight, dip2px(context, 20)); } if (drawableRight != null) { rightWidth = typedArray.getDimensionPixelOffset(R.styleable.DrawableTextView_rightDrawableWidth, dip2px(context, 20)); rightHeight = typedArray.getDimensionPixelOffset(R.styleable.DrawableTextView_rightDrawableHeight, dip2px(context, 20)); } if (drawableTop != null) { topWidth = typedArray.getDimensionPixelOffset(R.styleable.DrawableTextView_topDrawableWidth, dip2px(context, 20)); topHeight = typedArray.getDimensionPixelOffset(R.styleable.DrawableTextView_topDrawableHeight, dip2px(context, 20)); } } public int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (drawableLeft != null) { drawableLeft.setBounds(0, 0, leftWidth, leftHeight); } if (drawableRight != null) { drawableRight.setBounds(0, 0, rightWidth, rightHeight); } if (drawableTop != null) { drawableTop.setBounds(0, 0, topWidth, topHeight); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); this.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, null); } /** * 设置左侧图片并重绘 */ public void setDrawableLeft(Drawable drawableLeft) { this.drawableLeft = drawableLeft; invalidate(); } /** * 设置左侧图片并重绘 */ public void setDrawableLeft(int drawableLeftRes) { this.drawableLeft = mContext.getResources().getDrawable(drawableLeftRes); invalidate(); } /** * 设置右侧图片并重绘 */ public void setDrawableRight(Drawable drawableRight) { this.drawableRight = drawableLeft; invalidate(); } /** * 设置右侧图片并重绘 */ public void setDrawableRight(int drawableRightRes) { this.drawableRight = mContext.getResources().getDrawable(drawableRightRes); invalidate(); } /** * 设置上部图片并重绘 */ public void setDrawable(Drawable drawableTop) { this.drawableTop = drawableTop; invalidate(); } /** * 设置右侧图片并重绘 */ public void setDrawableTop(int drawableTopRes) { this.drawableTop = mContext.getResources().getDrawable(drawableTopRes); invalidate(); }} 本帖附件 点击下载 乱码三千 – 点滴积累 ,欢迎来到乱码三千技术博客站