如何将自定义视图插入XML的LinearLayout
|
所以我有一个ѭ0,它是从ѭ1扩展而来的。而且我有一个来自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?
没有找到相关结果
已邀请:
3 个回复
骨乏唯瓜
您传递的是布局的ID,而不是您创建的视图的ID。 您的其余代码看起来不错
缝皋
倪蕊悲潍