【问题标题】:How to the change BackgroundColor of a Button at runtime in Android?如何在 Android 运行时更改按钮的 BackgroundColor?
【发布时间】:2011-07-19 02:30:21
【问题描述】:

下面的代码将背景颜色更改为黑色,无论我传递什么颜色作为 论据。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    b = (Button)findViewById(R.id.Button01);
    b.setBackgroundColor(R.color.red);
}

有什么帮助吗?

【问题讨论】:

    标签: java android button colors


    【解决方案1】:

    使用这个:

    getResources().getColor()
    

    如下:

    b.setBackgroundColor(getResources().getColor(R.color.red));
    

    【讨论】:

      【解决方案2】:

      我知道这是一个很晚的答案,但我刚刚遇到了你的问题

      你可以使用

      b.setBackgroundColor(android.R.color.white)
      

      b.setBackgroundColor(Color.RED);
      

      【讨论】:

        猜你喜欢
        • 2021-03-25
        • 1970-01-01
        • 2021-08-09
        • 1970-01-01
        • 2012-07-07
        • 1970-01-01
        • 2019-06-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多