結論
val view: View = findViewById(R.id.constraint) // idを指定(synthetic使えば要らない)
view.background = getDrawable(R.color.colorAccent) // 背景色をcolorAccentに変更
全体
MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
constraint.background = getDrawable(R.color.colorAccent) // constraintの背景色をcolorAccentに変更
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/constraint"
android:layout_width="match_parent"
android:layout_height="match_parent" />