<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/btn_get_involved"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:text="@string/tv_xmllayoutexample_above"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2px"
android:background="#FFF"
android:layout_gravity="center">
<org.andengine.opengl.view.RenderSurfaceView android:id="@+id/xmllayoutexample_rendersurfaceview"
android:layout_width="240px"
android:layout_height="240px"
android:layout_margin="10px" />
</LinearLayout>
<TextView android:id="@+id/btn_get_involved"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:text="@string/tv_xmllayoutexample_below"/>
</LinearLayout>
</ScrollView>
在上面是一個布局檔案。開發過android的童鞋一看都知道幹嘛的,就不多說啦,下面就是看看如何在AndEngine中如何使用xml布局檔案:
@Override
protected int getLayoutID() {
return R.layout.xmllayoutexample;
}
@Override
protected int getRenderSurfaceViewID() {
return R.id.xmllayoutexample_rendersurfaceview;
}
在activity中要重寫如上的兩個方法,第一個是得到對該xml檔案本身的引用。第二個方法是得到該布局檔案中自訂的一個view控制項,也就是org.andengine.opengl.view.RenderSurfaceView的引用,無可厚非我們是採用得到其ID來得到該控制項的。
在Activity中重寫 protected Scene onCreateScene(){............}方法時,該情境是以布局檔案中自訂的View為基礎的,view的大小決定了scene的大小等。至於scene中要添加什麼就自己看著辦啦。