Anko中如何设置垂直布局中的按钮颜色

How to set the button color in Vertical Layout in Anko

我正在尝试使用 Anko 垂直布局的登录表单。尝试更改按钮的背景和文本颜色,如下所示。但它不起作用。

class LoginActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    LoginActivityUI().setContentView(this)   } }

class LoginActivityUI : AnkoComponent<LoginActivity> {

  override fun createView(ui: AnkoContext<LoginActivity>) = with(ui) {
    verticalLayout {
      padding = dip(22)
      var userName = editText()
      userName.hint = "Enter UserName"

      var passWord = editText()
      passWord.hint = "Enter Password"

      var button = button {
        backgroundColor = R.color.colorPrimary
        text = "SignIn"
        textColor = android.R.color.white
      }

    }   
}

}

终于找到了问题的答案:

backgroundColor = ContextCompat.getColor(context,R.color.colorPrimary)