android自定义视图FindViewWid为空且不可见
我正在尝试使用我自己的自定义视图,其中包含layout.xml文件。我多次使用此视图,但有时在Android Studio中预览时不会显示此视图。当我通过'findViewById'在活动中获得这个自定义视图时(事实上,我使用Kotlin,所以我只是直接写入Id),它总是返回null。 我读了很多关于“自定义视图findviewbyd返回null”的问题,直到现在我还没有找到答案。我怎么了 这是我的定制课程android自定义视图FindViewWid为空且不可见,android,kotlin,custom-view,findviewbyid,Android,Kotlin,Custom View,Findviewbyid,我正在尝试使用我自己的自定义视图,其中包含layout.xml文件。我多次使用此视图,但有时在Android Studio中预览时不会显示此视图。当我通过'findViewById'在活动中获得这个自定义视图时(事实上,我使用Kotlin,所以我只是直接写入Id),它总是返回null。 我读了很多关于“自定义视图findviewbyd返回null”的问题,直到现在我还没有找到答案。我怎么了 这是我的定制课程 class SubTitleBar(ctx:Context) : RelativeLay
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 = Context.LAYOUT_INFLATER_SERVICE
val inflater = ctx.getSystemService(infService) as LayoutInflater
val view = inflater.inflate(R.layout.cv_subtitle,this,false)
mTitle = view.findViewById(R.id.subtitle_title)
mSideBtn = view.findViewById(R.id.subtitle_btn)
addView(view)
}
private fun setAttributeSet(typedArray: TypedArray) {
val titleText = typedArray.getString(R.styleable.SubTitleBar_title)
mTitle.text = titleText
val btnText = typedArray.getString(R.styleable.SubTitleBar_btnText)
if (btnText!= null && btnText.isNotEmpty()) {
mSideBtn.text = btnText
} else {
mSideBtn.visibility = TextView.GONE
}
val showDivider = typedArray.getBoolean(R.styleable.SubTitleBar_showDivider, false)
if (showDivider) {
subtitle_divider.visibility = View.VISIBLE
}
typedArray.recycle()
}
fun setTitle(title:String) {
mTitle.text = title
}
fun setSideButtonClickListener(listener: (View) -> Unit) = mSideBtn.setOnClickListener(listener)
}
这是cv_subtitle.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="52dp"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/subtitle_title"
style="@style/SubTitleNormal"
android:paddingStart="@dimen/contentStartPadding"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/subtitle_btn"
style="@style/SubTitleSideBtn"
android:layout_alignParentEnd="true"
android:paddingEnd="@dimen/contentEndPadding"
android:layout_centerVertical="true"
/>
<View
android:id="@+id/subtitle_divider"
style="@style/PaddingDividerGray"
android:layout_alignParentBottom="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:visibility="gone"
/>
</RelativeLayout>
我也试过了,但不管用
val subTitle = view.findViewById(R.id.my_apt_list_subtitle)
尝试在cv_subtitle.xml中包含字幕栏,您需要传递属性,其中包括超级类型的id。你必须改变
class SubTitleBar(ctx:Context) : RelativeLayout(ctx) {
到
并相应地更改其他构造函数。对不起,我迟到了-。。我想用xml文件扩展自定义视图。我无法理解您在cv_subtitle.xml文件中包含字幕栏的目的。因为cv_subtitle.xml是字幕栏(我的自定义视图)的xml布局。
val subTitle = view.findViewById(R.id.my_apt_list_subtitle)
class SubTitleBar(ctx:Context) : RelativeLayout(ctx) {
class SubTitleBar(ctx:Context, attrs: AttributeSet) : RelativeLayout(ctx, attrs) {