TextView Button setCompoundDrawa

作者: 钟金宝 | 来源:发表于2017-08-02 09:55 被阅读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)

相关文章

网友评论

    本文标题:TextView Button setCompoundDrawa

    本文链接:https://www.haomeiwen.com/subject/efrelxtx.html