Android 动态setter自定义视图
我开始在Android上创建图形组件,我面临一个我认为非常简单的问题 传递给我的自定义布局的属性在XML中被读取和识别。 但是,我希望动态传递这些属性 这是我的密码:Android 动态setter自定义视图,android,kotlin,android-custom-view,Android,Kotlin,Android Custom View,我开始在Android上创建图形组件,我面临一个我认为非常简单的问题 传递给我的自定义布局的属性在XML中被读取和识别。 但是,我希望动态传递这些属性 这是我的密码: <declare-styleable name="MyAppBarLayout"> <attr name="text" format="string" /> </declare-styleable> 我想对该视图执行以下操作: var myAppBar.text
<declare-styleable name="MyAppBarLayout">
<attr name="text" format="string" />
</declare-styleable>
我想对该视图执行以下操作:
var myAppBar.text = "WARNING"
如何设置动态设置器?
我已经看过谷歌关于这个主题的文档,但我不明白是否应该有一个与属性对应的属性。一种方法是:
class MyAppBarLayout(context: Context, attrs: AttributeSet): AppBarLayout(context, attrs) {
private var text: String? = null
...
fun setText(text: String) {
view.sutitle.text = text
}
}
// wherever you want to set the text
var myAppBar.text = "WARNING"
您的做法是从布局xml设置文本(属性)。我不理解您的代码。。。如果它是私有的,我不能从活动中访问它。你可以将它公开,或者为它设置一个setter。将使用setter函数更新代码。您正在寻找一个简单的
var
和set(value){
class MyAppBarLayout(context: Context, attrs: AttributeSet): AppBarLayout(context, attrs) {
private var text: String? = null
...
fun setText(text: String) {
view.sutitle.text = text
}
}
// wherever you want to set the text
var myAppBar.text = "WARNING"