Calling setCompoundDrawables() doesn't display the Compound Drawable

后端 未结 10 1026
自闭症患者
自闭症患者 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 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)
    

提交回复
热议问题