因此,我有一個CustomView
從View
擴展。我有一個來自XML的線性佈局。 命名爲example
的XML:如何將自定義視圖插入XML的LinearLayout
<?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?
你實際使用完整的包? – Kakey 2011-05-31 11:18:45
使用合併標籤而不是頂級的線性佈局 – vnshetty 2011-05-31 11:24:14
@Jinda恩,恩,我看着http://developer.android.com/resources/samples/ApiDemos/res/layout/custom_view_1.html,它給出了完整的包。 – dieend 2011-05-31 11:32:02