Android 如何在自定义视图中获取作为引用给定的组件?
嗨,我正在制作一个自定义视图,并在其中提供其他组件id作为参考,如下所示Android 如何在自定义视图中获取作为引用给定的组件?,android,custom-component,Android,Custom Component,嗨,我正在制作一个自定义视图,并在其中提供其他组件id作为参考,如下所示 <com.example.timerview.CustomView android:id="@+id/custom_view" android:layout_width="100dip" android:layout_height="100dip" mtv:associatedTextView="@+id/text_view" /> <TextView andr
<com.example.timerview.CustomView
android:id="@+id/custom_view"
android:layout_width="100dip"
android:layout_height="100dip"
mtv:associatedTextView="@+id/text_view"
/>
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
但是我发现
tText
是null
请告诉我哪里错了或者怎么做。请帮助我。您的文本视图不是自定义视图的子视图,这就是为什么findViewById
无法找到它的原因
附加自定义视图后,从根视图调用findViewById
:
protected void onAttachedToWindow() {
super.onAttachedToWindow();
int tId = a.getResourceId(R.styleable.CustomView_associatedTextView, 0);
TextView tText = (TextView) getRootView().findViewById(tId);
}
findviewbyd(R.id.text\u视图)有什么问题代码>?其也返回null
protected void onAttachedToWindow() {
super.onAttachedToWindow();
int tId = a.getResourceId(R.styleable.CustomView_associatedTextView, 0);
TextView tText = (TextView) getRootView().findViewById(tId);
}