如何在Java代码中设置Button的drawableLeft属性

时间:2015-01-16 15:55:09

标签: android

点击时,按钮类的 drawableLeft 属性必须更改为其他图像。

以下xml内容的等效java代码是什么。

<Button
    android:drawableLeft="@drawable/play_button />

2 个答案:

答案 0 :(得分:1)

使用此

Drawable drawable = getResources().getDrawable(R.drawable.play_button);

button.setCompoundDrawables(drawable, null, null, null);

//按钮是你的按钮。

答案 1 :(得分:0)

试试这个:

Bitmap b = BitmapFactory.decodeResource(context.getResources(), R.drawable.play_button);
Drawable d = new BitmapDrawable(context.getResources(),b);
    yourButtonName.setCompoundDrawablesWithIntrinsicBounds(d, null , null , null );