addview到布局

时间:2012-11-23 20:21:04

标签: android layout

我创建了扩展View的类,我在布局中尝试layout.addView(something),而不是抛出异常。

布局

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RelativeLayout>

public class Navigator extends View {
Context context;
RelativeLayout layout = (RelativeLayout) findViewById(R.layout.navigator);

public Navigator(Context context) {
    super(context);
    this.context = context;
}

TextView tab1 = new TextView(context);
tab1.setText("blah");
PROBLEM-> layout.addView(tab1);
}

1 个答案:

答案 0 :(得分:0)

您是否将扩展视图添加到活动中?

因为您正在修改视图,但我无法看到您在哪里附加了&#34; Navigator&#34;图。

另一件事是你在XML上声明的RelativeLayout没有ID,所以你永远不会找到一个带有R.id.navigator id的RelativeLayout。

相关问题