在代码中可以设置Drawable显示在text的左、上、右、下位置,有如下两个方法:
setCompoundDrawables(left, top, right, bottom)
setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
1、setCompoundDrawables(left, top, right, bottom)
drawable的宽高是按drawable.setBound()设置的宽高,
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use {@code null} if you do not want a Drawable there. The Drawables must already have had {@link Drawable#setBounds} called.
使用之前必须使用Drawable.setBounds设置Drawable的长宽。
2、setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
drawable的宽高是按drawable固定的宽高,
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use {@code null} if you do not want a Drawable there. The Drawables’ bounds will be set to their intrinsic bounds.
即通过getIntrinsicWidth()与getIntrinsicHeight()获得,
直接使用setCompoundDrawablesWithIntrinsicBounds代替setCompoundDrawables 在Java代码中使用就可以正常显示。