以编程方式更改Button的drawableLeft

das*_*shh 7 android image button textview drawable

我正在使用Button

<Button
        android:id="@+id/zoom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/trans"
        android:drawableLeft="@drawable/left_img"
        android:fontFamily="arial"
        android:text="My Name is "
        android:textSize="50sp" />
Run Code Online (Sandbox Code Playgroud)

并改变其文字颜色:

zoom.setTextColor(Color.parseColor("voilet"));
Run Code Online (Sandbox Code Playgroud)

但不能理解 how to change its image??

Tul*_*lio 39

试试这个:

int imgResource = R.drawable.left_img;
button.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)

参考


Jor*_*dan 12

设置左侧drawable而不更改其他drawable(顶部,右侧和底部)的值的最安全方法:

Drawable[] drawables = textViewExample.getCompoundDrawables();
textViewExample.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, drawables[1], drawables[2], drawables[3]);
Run Code Online (Sandbox Code Playgroud)