以编程方式设置 AppCompatBackgroundTint

Set AppCompatBackgroundTint programmatically

我有一个 TextView,我想为其设置背景色调。我的应用程序有一个 minSDK19,所以我使用 AppCompatTextView 和 app:backgroundTint 参数来以声明方式设置色调。我想以编程方式更改色调,但 setSupportBackgroundTintList() 方法在库组之外无法访问。如何从 Activity?

更改此值

来自 androidx.appcompat.widget.AppCompatTextView 的源代码:

This should be accessed via

ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)

所以,这样写:

AppCompatTextView textView = findViewById(R.id.your_text_view)
ColorStateList colorStateList = ContextCompat.getColorStateList(this, R.color.your_state_list);
ViewCompat.setBackgroundTintList(textView, colorStateList);