【发布时间】:2011-05-31 11:14:16
【问题描述】:
所以我有一个从View 扩展而来的CustomView。我有一个来自 XML 的线性布局。
名为example的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/jembalang.comfest.game"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<jembalang.compfest.game.GameThread
android:id="@+id/game_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</jembalang.compfest.game.GameThread>
<Button
android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
以及使用xml的代码
setContentView(R.layout.cobagabung);
gameView = (GameThread) Jembalang.this.findViewById(R.id.game_view);
gameView.setFocusable(true);
gameView.setFocusableInTouchMode(
gameView.start();
如果有帮助,我添加了 GameThread 构造函数
public class GameThread extends View implements Runnable,OnKeyListener{
public GameThread(Context context,AttributeSet attr){
super(context, attr);
...
}
public GameThread(Context context) {
super(context);
...
}
我认为我的做法有问题,因为findViewById 返回null
我应该怎么做才能让我的CustomView(在这个例子中是GameThread)能够插入到xml中?
【问题讨论】:
-
你真的在使用完整的包吗?
-
在顶层使用合并标签而不是线性布局
-
@Jinda 嗯,是的,我在看developer.android.com/resources/samples/ApiDemos/res/layout/…,它提供了完整的包。
-
@vnshetty 不,它仍然没有工作:(
-
@dieend 对不起,我无法找到你失踪的地方。你最好用断点调试你的代码。当你的 setContentView 被执行时,你的自定义视图应该被初始化......
标签: android view android-layout android-xml android-custom-view