如何以编程方式更改按钮的图标? [英] How to Change a Button's Icon Programmatically?

查看:28
本文介绍了如何以编程方式更改按钮的图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有了按钮:

<Button 
android:layout_height="wrap_content" 
android:layout_width="fill_parent"
android:drawableLeft="@drawable/empty"
android:id="@+id/buttonMyText" 
android:text="  myText" 
android:textSize="20px" 
android:gravity="left">
</Button>

当程序启动时,按钮上会显示空"图标.

I have the "empty" icon show on the button when the program starts.

我想要做的是根据用户输入从我的代码(低、中和高)中自动更改按钮的图标

What I want to do is change the button's icon automatically from my code (low, medium and high) based on user inputs

我试过了:

Button myButton = bla... bla... bla...

但是我不知道

myButton.(what?)

推荐答案

如果您查看文档,您将看到每个 XML 属性的等效代码.

If you check the docs, you'll see the code equivalent for each XML attribute.

参见此处:http://developer.android.com/reference/android/widget/Button.html

搜索 drawableLeft 显示:

android:drawableLeft:
setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)

这篇关于如何以编程方式更改按钮的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆