Android知识Android开发经验谈Android开发

TextView Button setCompoundDrawa

2017-08-02  本文已影响51人  钟金宝

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)
上一篇 下一篇

猜你喜欢

热点阅读