以编程方式更新自定义视图自定义属性

Update custom view custom properties programmatically

我有一个名为 CustomView 的自定义视图,它定义了两个自定义属性 att1 和 att2。

在 XML 中,我可以像下面这样轻松更新属性:

<com.example.myapp.CustomView
     android:id="@+id/custom_view"
     app:att1="value_1"
     app:att2=value_2"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>

但是如何以编程方式更新它们?

与此类似:

val customView: CustomView = findViewById(R.id.custom_view)
customView.att1 = "value_1"
customView.att2 = "value2"

@DarShan in the comment, creating setter and getter and invalidating the view was the solution. You can refer to 问题所述。