Calculate image size when using setCompoundDrawables for EditText

后端 未结 3 498
深忆病人
深忆病人 2021-01-17 17:16

When I am add icon like below:

etComment = (EditText) findViewById(R.id.et_comment);
Drawable img = getResources().getDrawable( R.drawable.warning );
etComme         


        
相关标签:
3条回答
  • 2021-01-17 17:52

    I've found the solution in this post.

    Drawable dr = this.getResources().getDrawable(R.drawable.warning);
    Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
    Drawable d = new BitmapDrawable(this.getResources(), Bitmap.createScaledBitmap(bitmap, 35, 35, true));
    
    txtValue.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
    

    HTH, Milton

    0 讨论(0)
  • 2021-01-17 18:00

    I think you can use different size of pics for different screens and use getMinimumWidth to set Bounds.But I did not try it before , may be it is not appropriate for .9 patch.

    When you use setCompoundDrawables , you need code like :

    Drawable img;
    Resources res = getResources();
    img = res.getDrawable(R.drawable.btn_img);
    //You need to setBounds before setCompoundDrawables , or it couldn't display
    img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
    btn.setCompoundDrawables(img_off, null, null, null); 
    
    0 讨论(0)
  • 2021-01-17 18:08

    Use setCompoundDrawables() instead of setCompoundDrawablesWithIntrinsicBounds() - you'll have to set the bounds of the drawables manually.

    0 讨论(0)
提交回复
热议问题