Andengine中如何使用xml檔案來布局

來源:互聯網
上載者:User

<?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中要添加什麼就自己看著辦啦。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.