Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 动态setter自定义视图_Android_Kotlin_Android Custom View - Fatal编程技术网

Android 动态setter自定义视图

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

我开始在Android上创建图形组件,我面临一个我认为非常简单的问题

传递给我的自定义布局的属性在XML中被读取和识别。 但是,我希望动态传递这些属性

这是我的密码:

    <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"