TextView Button setCompoundDrawables()无法显示的问题

StackOverFlow回答

Image is blank because it hasn't got specified bounds. You may use setCompoundDrawables() but before you should specify image's bounds, using Drawable.setBounds() method

使用setCompoundDrawablesWithIntrinsicBounds方法可以使图片正常显示。

如果想要改变图片显示的位置,大小可以使用这段代码:

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 );

/**
* Specify a bounding rectangle for the Drawable. This is where the drawable
* will draw when its draw() method is called.
*/   
void setBounds(int left, int right, int top, int bottom)

你可能感兴趣的:(TextView Button setCompoundDrawables()无法显示的问题)