【发布时间】:2021-02-09 12:57:16
【问题描述】:
我有一个扩展RelativeLayout 的自定义视图。我在attrs.xml 中有一些自定义属性。有没有办法像我们使用自定义属性一样获取常见的android attrs,如android:clickable?
class MyView(context: Context, attributeSet: AttributeSet?): RelativeLayout(context, attributeSet) {
init {
val attrs = context.obtainStyledAttributes(attributeSet, R.styleable.MyView)
if (attrs.getBoolean(android.R.attr.clickable, true)) {
...
}
attrs.recycle()
}
}
此编译器接受,但它在运行时崩溃。有人遇到过类似的用例吗?我想避免为 SDK 中已经定义的自定义视图创建重复的属性。
【问题讨论】:
-
通过扩展
RelativeLayout,自定义视图继承了它的属性。您通常不需要“触摸”它们,除非您的意图是改变父母的行为,这实际上可能会让用户/开发人员感到困惑/不可预测。 -
为什么会崩溃?什么是堆栈跟踪?
标签: java android xml kotlin view