我有一个自定义的线性布局。它包含一个文本框和一些其他元素。
<a.b.c.CustomLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
/>
</a.b.c.CustomLinearLayout>
我有以下类,从中我尝试以下方式访问txtitle元素:
public class CustomLinearLayout extends LinearLayout
{
public CustomLinearLayout( Context context, AttributeSet attrs )
{
super( context, attrs );
TextView txtTitle = (TextView)findViewById( R.id.txtTitle );
}
}
但是txtitle总是空的。我也试着这样访问它,但没有任何运气:
TextView txtTitle = (TextView)this.findViewById( R.id.txtTitle );
TextView txtTitle = (TextView)((Activity)context).findViewById( R.id.txtTitle );
TextView txtTitle = (TextView)context.findViewById( R.id.txtTitle );
如何从上下文访问TextView?谢谢您!