Tag: 自定义查看

android自定义视图findviewId为null并且不可见

我试图用我自己的自定义视图充满layout.xml文件。 我多次使用此视图,但有时在Android Studio中不会显示预览。 而当我通过’findViewById’得到这个自定义视图(实际上,我使用Kotlin所以我直接写Id),它总是返回null。 我读了很多关于“自定义视图findviewById返回null”的问题,直到现在我没有find答案。 我错了什么? 这是我的自定义类 class SubTitleBar(ctx:Context) : RelativeLayout(ctx) { private val mTitle:TextView private val mSideBtn:TextView constructor(ctx: Context, attrs: AttributeSet) : this(ctx) { val typedArray = context.obtainStyledAttributes(attrs, R.styleable.SubTitleBar) setAttributeSet(typedArray) } constructor(ctx: Context, attrs: AttributeSet, defStyle:Int) : this(ctx, attrs) { val typedArray = context.obtainStyledAttributes(attrs, R.styleable.SubTitleBar, defStyle, 0) setAttributeSet(typedArray) } init { val infService = […]