2014-04-25 76 views
1

在开始时,我将内容视图设置为一个新的类,它扩展了SurfaceView(请参阅示例)。在SurfaceView上添加布局

RenderView surfaceView = new RenderView(); 
setContentView(surfaceView); 

但我似乎无法能够添加从XML文件中的布局,因为充气要求我加入的是某种看法的第一层,并引发我一个错误,当我尝试夸大像我会做一个正常的方式。

所以问题是,如何在SurfaceView上添加一些来自.xml文件的布局?

这是的RenderView类的代码的重要组成部分:

public class RenderView extends SurfaceView implements Runnable { 

Context context; 
Thread thread = null; 

SurfaceHolder holder; 
boolean running = false; 

public RenderView(Context context) { 
    super(context); 
    this.context = context; 
    holder = getHolder(); 
} 

public void run() { 
    while (running) { 
     if(!holder.getSurface().isValid()) continue; 
     Canvas c = holder.lockCanvas(); 


     holder.unlockCanvasAndPost(c); 
    } 
} 
+0

如果在该RenderView上添加了View()会怎么样? – shkschneider

+0

我想从.xml文件加载它。 – ViliX64

+0

您应该能够将null传递给LayoutInflater。你说错误发生,请张贴它的堆栈跟踪。 – shkschneider

回答

1

如果您希望以xml格式显示所有内容,请在框架布局中环绕表面视图。我在我的游戏中做到了这一点,我已经堆焊分数和时间一顶栏就可以了:

<LinearLayout 
    android:id="@+id/topBar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/scoreText" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="#F7F7F7" 
     android:ems="10" 
     android:gravity="center" 
     android:paddingLeft="5dp" 
     android:textColor="#000000" 
     android:textSize="20sp" > 
    </TextView> 

    <TextView 
     android:id="@+id/timeText" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="#F7F7F7" 
     android:ems="10" 
     android:gravity="center" 
     android:paddingLeft="5dp" 
     android:textColor="#000000" 
     android:textSize="20sp" > 
    </TextView> 
</LinearLayout> 

更改/调整高度和定位等您的需求。