在我调用setCompoundDrawables方法后,复合Drawable没有显示。

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

任何想法吗?


当前回答

图像是空白的,因为它没有指定的边界。你可以使用setCompoundDrawables(),但在指定图像的边界之前,使用Drawable.setBounds()方法

其他回答

再简单一点:

Drawable image = context.getResources().getDrawable(R.drawable.ic_action );
image.setBounds( 0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight() );
button.setCompoundDrawables( image, null, null, null );

使用这个(我测试过)。效果很好

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

图像是空白的,因为它没有指定的边界。你可以使用setCompoundDrawables(),但在指定图像的边界之前,使用Drawable.setBounds()方法

我需要使用setCompoundDrawablesWithIntrinsicBounds。

对我来说,setCompoundDrawablesWithIntrinsicBounds(Drawable, Drawable, Drawable, Drawable)不工作。

我必须使用setcompounddrawableswithintrinsic icbounds(0,0,0,0)。