Is it possible to scale drawableleft & drawableright in textview?

前端 未结 8 2226
悲&欢浪女
悲&欢浪女 2020-12-14 05:53

I have TextView with drawableLeft & drawableRight in List item. The problem is, whenever the height of TextView is l

8条回答
  •  被撕碎了的回忆
    2020-12-14 06:02

    Hope this help

    Textview tv = (TextView) findViewById(R.id.tv_dummy)
    int imageResource =  R.mipmap.ic_image;
    Drawable drawable = ContextCompat.getDrawable(context, imageResource);
    int pixelDrawableSize = (int)Math.round(tv.getLineHeight() * 0.7); // Or the percentage you like (0.8, 0.9, etc.)
    drawable.setBounds(0, 0, pixelDrawableSize, pixelDrawableSize); // setBounds(int left, int top, int right, int bottom), in this case, drawable is a square image
    tv.setCompoundDrawables(
     null, //left
     null, //top
     drawable, //right
     null //bottom
    );
    

提交回复
热议问题