Android以编程方式设置按钮背景 [英] android set button background programmatically

查看:66
本文介绍了Android以编程方式设置按钮背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何以编程方式设置按钮颜色? 我已经编写了以下代码,但失败了:

I would like to know how to set the button color programatically? I have coded the following but fails:

Button11.setBackgroundColor(R.color.red);

谢谢!

推荐答案

R.color.red是ID(也是int),但不是颜色.

R.color.red is an ID (which is also an int), but is not a color.

改为使用以下之一:

// If you're in an activity:
Button11.setBackgroundColor(getResources().getColor(R.color.red));
// OR, if you're not: 
Button11.setBackgroundColor(Button11.getContext().getResources().getColor(R.color.red));

或者,或者:

Button11.setBackgroundColor(Color.RED); // From android.graphics.Color

或者,要获得更多专业技能:

Or, for more pro skills:

Button11.setBackgroundColor(0xFFFF0000); // 0xAARRGGBB

这篇关于Android以编程方式设置按钮背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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