From 6fc2dc5ed9341f645419f58ad51097369ebd822f Mon Sep 17 00:00:00 2001 From: folecr Date: Thu, 24 May 2012 17:12:48 -0700 Subject: [PATCH] Use Java code instead of XML to define layout * Android layout for cocos apps are very simple (full screen) * Avoids creation of R.java for loading the layout * Can package entire library in jar * Remove unused layout resource --- .../src/org/cocos2dx/hellolua/HelloLua.java | 30 ++++++++++++++- .../cocos2dx/application/ApplicationDemo.java | 32 ++++++++++++++-- .../layout/cocos2dx_default_screen_layout.xml | 16 -------- template/android/copy_files.sh | 1 + .../src/org/cocos2dx/tests/TestsDemo.java | 37 ++++++++++++++++--- 5 files changed, 90 insertions(+), 26 deletions(-) delete mode 100644 cocos2dx/platform/android/java/res/layout/cocos2dx_default_screen_layout.xml diff --git a/HelloLua/proj.android/src/org/cocos2dx/hellolua/HelloLua.java b/HelloLua/proj.android/src/org/cocos2dx/hellolua/HelloLua.java index b8cb76b3de..7bc6482867 100644 --- a/HelloLua/proj.android/src/org/cocos2dx/hellolua/HelloLua.java +++ b/HelloLua/proj.android/src/org/cocos2dx/hellolua/HelloLua.java @@ -23,6 +23,7 @@ THE SOFTWARE. ****************************************************************************/ package org.cocos2dx.hellolua; import org.cocos2dx.lib.Cocos2dxActivity; +import org.cocos2dx.lib.Cocos2dxEditText; import org.cocos2dx.lib.Cocos2dxGLSurfaceView; import org.cocos2dx.lib.Cocos2dxRenderer; @@ -33,6 +34,8 @@ import android.opengl.GLSurfaceView; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; +import android.widget.FrameLayout; +import android.view.ViewGroup; public class HelloLua extends Cocos2dxActivity{ protected void onCreate(Bundle savedInstanceState){ @@ -43,10 +46,35 @@ public class HelloLua extends Cocos2dxActivity{ String packageName = getApplication().getPackageName(); super.setPackageName(packageName); + // FrameLayout + ViewGroup.LayoutParams framelayout_params = + new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, + ViewGroup.LayoutParams.FILL_PARENT); + FrameLayout framelayout = new FrameLayout(this); + framelayout.setLayoutParams(framelayout_params); + + // Cocos2dxEditText layout + ViewGroup.LayoutParams edittext_layout_params = + new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + Cocos2dxEditText edittext = new Cocos2dxEditText(this); + edittext.setLayoutParams(edittext_layout_params); + + // ...add to FrameLayout + framelayout.addView(edittext); + + // LuaGLSurfaceView mGLView = new LuaGLSurfaceView(this); - setContentView(mGLView); + + // ...add to FrameLayout + framelayout.addView(mGLView); + mGLView.setEGLContextClientVersion(2); mGLView.setCocos2dxRenderer(new Cocos2dxRenderer()); + mGLView.setTextField(edittext); + + // Set framelayout as the content view + setContentView(framelayout); } else { Log.d("activity", "don't support gles2.0"); diff --git a/HelloWorld/proj.android/src/org/cocos2dx/application/ApplicationDemo.java b/HelloWorld/proj.android/src/org/cocos2dx/application/ApplicationDemo.java index 331d161346..eb39238aac 100644 --- a/HelloWorld/proj.android/src/org/cocos2dx/application/ApplicationDemo.java +++ b/HelloWorld/proj.android/src/org/cocos2dx/application/ApplicationDemo.java @@ -33,6 +33,8 @@ import android.content.Context; import android.content.pm.ConfigurationInfo; import android.os.Bundle; import android.util.Log; +import android.widget.FrameLayout; +import android.view.ViewGroup; public class ApplicationDemo extends Cocos2dxActivity{ private Cocos2dxGLSurfaceView mGLView; @@ -45,11 +47,35 @@ public class ApplicationDemo extends Cocos2dxActivity{ String packageName = getApplication().getPackageName(); super.setPackageName(packageName); - setContentView(R.layout.cocos2dx_default_screen_layout); - mGLView = (Cocos2dxGLSurfaceView) findViewById(R.id.cocos2dx_default_gl_surfaceview); - mGLView.setTextField((Cocos2dxEditText)findViewById(R.id.cocos2dx_default_textField)); + // FrameLayout + ViewGroup.LayoutParams framelayout_params = + new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, + ViewGroup.LayoutParams.FILL_PARENT); + FrameLayout framelayout = new FrameLayout(this); + framelayout.setLayoutParams(framelayout_params); + + // Cocos2dxEditText layout + ViewGroup.LayoutParams edittext_layout_params = + new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + Cocos2dxEditText edittext = new Cocos2dxEditText(this); + edittext.setLayoutParams(edittext_layout_params); + + // ...add to FrameLayout + framelayout.addView(edittext); + + // Cocos2dxGLSurfaceView + mGLView = new Cocos2dxGLSurfaceView(this); + + // ...add to FrameLayout + framelayout.addView(mGLView); + mGLView.setEGLContextClientVersion(2); mGLView.setCocos2dxRenderer(new Cocos2dxRenderer()); + mGLView.setTextField(edittext); + + // Set framelayout as the content view + setContentView(framelayout); } else { Log.d("activity", "don't support gles2.0"); diff --git a/cocos2dx/platform/android/java/res/layout/cocos2dx_default_screen_layout.xml b/cocos2dx/platform/android/java/res/layout/cocos2dx_default_screen_layout.xml deleted file mode 100644 index cfc9bc4975..0000000000 --- a/cocos2dx/platform/android/java/res/layout/cocos2dx_default_screen_layout.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - diff --git a/template/android/copy_files.sh b/template/android/copy_files.sh index eadbe68fbb..5fa8148cfe 100644 --- a/template/android/copy_files.sh +++ b/template/android/copy_files.sh @@ -6,6 +6,7 @@ APP_NAME=$2 COCOS2DX_ROOT=$1 APP_DIR=$COCOS2DX_ROOT/$APP_NAME HELLOWORLD_ROOT=$COCOS2DX_ROOT/HelloWorld +COCOSJAVALIB_ROOT=$COCOS2DX_ROOT/cocos2dx/platform/android/java NDK_ROOT=$3 PACKAGE_PATH=$4 NEED_BOX2D=$5 diff --git a/tests/proj.android/src/org/cocos2dx/tests/TestsDemo.java b/tests/proj.android/src/org/cocos2dx/tests/TestsDemo.java index 2c15ed0abd..08652a53ec 100644 --- a/tests/proj.android/src/org/cocos2dx/tests/TestsDemo.java +++ b/tests/proj.android/src/org/cocos2dx/tests/TestsDemo.java @@ -33,6 +33,8 @@ import android.content.Context; import android.content.pm.ConfigurationInfo; import android.os.Bundle; import android.util.Log; +import android.widget.FrameLayout; +import android.view.ViewGroup; public class TestsDemo extends Cocos2dxActivity{ private Cocos2dxGLSurfaceView mGLView; @@ -45,12 +47,35 @@ public class TestsDemo extends Cocos2dxActivity{ String packageName = getApplication().getPackageName(); super.setPackageName(packageName); - setContentView(R.layout.cocos2dx_default_screen_layout); - - mGLView = (Cocos2dxGLSurfaceView) findViewById(R.id.cocos2dx_default_gl_surfaceview); - mGLView.setTextField((Cocos2dxEditText)findViewById(R.id.cocos2dx_default_textField)); - mGLView.setEGLContextClientVersion(2); - mGLView.setCocos2dxRenderer(new Cocos2dxRenderer()); + // FrameLayout + ViewGroup.LayoutParams framelayout_params = + new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, + ViewGroup.LayoutParams.FILL_PARENT); + FrameLayout framelayout = new FrameLayout(this); + framelayout.setLayoutParams(framelayout_params); + + // Cocos2dxEditText layout + ViewGroup.LayoutParams edittext_layout_params = + new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + Cocos2dxEditText edittext = new Cocos2dxEditText(this); + edittext.setLayoutParams(edittext_layout_params); + + // ...add to FrameLayout + framelayout.addView(edittext); + + // Cocos2dxGLSurfaceView + mGLView = new Cocos2dxGLSurfaceView(this); + + // ...add to FrameLayout + framelayout.addView(mGLView); + + mGLView.setEGLContextClientVersion(2); + mGLView.setCocos2dxRenderer(new Cocos2dxRenderer()); + mGLView.setTextField(edittext); + + // Set framelayout as the content view + setContentView(framelayout); } else { Log.d("activity", "don't support gles2.0");