gpt4 book ai didi

android以编程方式设置按钮背景

转载 作者:IT老高 更新时间:2023-10-28 21:35:53 26 4
gpt4 key购买 nike

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

Button11.setBackgroundColor(R.color.red);

谢谢!!

最佳答案

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

改用以下方法之一:

// 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

或者,更多专业技能:

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

关于android以编程方式设置按钮背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13842447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com