Calling setCompoundDrawables() doesn't display the Compound Drawable

后端 未结 10 1040
自闭症患者
自闭症患者 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:46

    The Image is not shown as you didn't specify the bounds, so you have 2 options here.

    1st Method

    Use setCompoundDrawablesWithIntrinsicBounds method, as shown below

    Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
    btn. setCompoundDrawablesWithIntrinsicBounds(myDrawable, null, null, null);
    

    2nd Method

    You can apply bounds to the drawable before applying to the TextView, as shown below

    Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
    myDrawable.setBounds( 0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
    btn.setCompoundDrawables(myDrawable, null, null, null);
    

    That's it.

提交回复
热议问题