如何插入自定义视图转换成XML的LinearLayout中 [英] How to insert custom view into XML's LinearLayout

查看:152
本文介绍了如何插入自定义视图转换成XML的LinearLayout中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 CustomView 这是从扩展视图。而我从XML的线性布局。
名为XML 例如

So I have a CustomView which is extended from View. And I have a linear layout from XML. The XML named example:

<?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的code

And the code using the xml

setContentView(R.layout.cobagabung);
gameView = (GameThread) Jembalang.this.findViewById(R.id.game_view);
gameView.setFocusable(true);
gameView.setFocusableInTouchMode(
gameView.start();

我添加了GameThread构造,如果这是帮助

I added the GameThread constructor if that's helping

public class GameThread extends View implements Runnable,OnKeyListener{
    public GameThread(Context context,AttributeSet attr){
        super(context, attr);
        ...
    }
    public GameThread(Context context) {
        super(context);
        ...
    }

我觉得有什么毛病我的方式做,因为 findViewById 收益
我应该怎样做才能让我的 CustomView GameThread 在这个例子中)能够插入XML?

I think there is something wrong with my way doing it, because the findViewById returns null How should I do to make my CustomView (GameThread at this example) to be able inserted into xml?

推荐答案

您说您的布局文件名为的example.xml 的,但你调用的的setContentView(R.layout。< STRONG> cobagabung )的。因此,你的看法是从初始化的 cobagabung.xml 的。

You say your layout file is called "example.xml", but you call setContentView(R.layout.cobagabung). Thus, your view is initialized from "cobagabung.xml".

请确保您使用的排版文件名和调用的setContentView相同的标识符,如

Make sure you use the same identifier for layout file name and setContentView call, like

setContentView(R.layout.example);

这篇关于如何插入自定义视图转换成XML的LinearLayout中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆