Tag: android custom view

Kotlin:如何访问自定义视图的Attrs

我在Kotlin中创建一个自定义视图,并想访问它的属性资源。 以下是我的代码 class CustomCardView : FrameLayout { constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet) : super(context, attrs) constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) init { LayoutInflater.from(context).inflate(R.layout.view_custom_card, this, true) if (attrs != null) { val a = context.obtainStyledAttributes(attrs, R.styleable.custom_card_view) if (a.hasValue(R.styleable.custom_card_view_command)) { var myString = a.getString(R.styleable.custom_card_view_command) } } } } 请注意,这将在init函数中的attrs错误。 […]