Calling setCompoundDrawables() doesn't display the Compound Drawable

后端 未结 10 985
自闭症患者
自闭症患者 2020-12-04 06:58

After I call the setCompoundDrawables method, the compound Drawable is not shown..

Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setC         


        
相关标签:
10条回答
  • 2020-12-04 07:56

    Use This (I tested). It works good

    Drawable image = context.getResources().getDrawable( R.drawable.ic_action );
    int h = image.getIntrinsicHeight(); 
    int w = image.getIntrinsicWidth();   
    image.setBounds( 0, 0, w, h );
    button.setCompoundDrawables( image, null, null, null );
    
    0 讨论(0)
  • 2020-12-04 08:02

    I needed to be using setCompoundDrawablesWithIntrinsicBounds.

    0 讨论(0)
  • 2020-12-04 08:02

    It is deprecated in API 22.

    This code is useful for me:

    Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.wen, null);
    drawable.setBounds(0, 0, drawable.getMinimumWidth(),
    drawable.getMinimumHeight());
    tv.setCompoundDrawables(drawable, null, null, null);
    
    0 讨论(0)
  • 2020-12-04 08:02

    In Kotlin:

    1) Set drawable:

    val drawable = ContextCompat.getDrawable(context!!,R.drawable.ic_image)?.apply {
        setBounds(0, 0, intrinsicWidth, intrinsicHeight)
    }
    

    or

    val drawable = ResourcesCompat.getDrawable(resources, R.drawable.ic_image, null)?.apply {
        setBounds(0, 0, minimumWidth, minimumHeight)
    }
    

    2) Set TextView:

    textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
    

    or

    button.setCompoundDrawables(null, drawable, null, null)
    
    0 讨论(0)
提交回复
热议问题