
Androidでレイアウトを動的に配置するサンプル@tryDynamicLayout00

動的にコントロールを配置してレイアウトを作る
main.xmlは使わない。
動的にGLSurfaceViewをはめ込む。
画面いっぱいにボタンを配置
●大事なところ
GLSurfaceViewのレイアウトでlayout_weightを設定する
●関連項目
setLayoutParams
setOrientation
LinearLayout.LayoutParams.FILL_PARENT
LinearLayout.LayoutParams.WRAP_CONTENT
addView
LinearLayout
LinearLayout.VERTICAL
開発環境
Eclipse IDE バージョン: 3.7 Indigo Service Release 1
ターゲットプラットフォーム: 2.3.3
API レベル: 10
package trial.sample.trydynamiclayout00; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.opengl.GLES20; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.util.AttributeSet; import android.view.Window; import android.widget.Button; import android.widget.LinearLayout; import android.widget.RelativeLayout; public class TryDynamicLayout00Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // タイトルは無し this.requestWindowFeature(Window.FEATURE_NO_TITLE); // レイアウトを作って設定 LinearLayout layout = new LinearLayout(this); layout.setBackgroundColor(Color.rgb(255, 255, 0)); layout.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); layout.setOrientation(LinearLayout.VERTICAL); this.setContentView(layout); // ボタンを設定する final Button button = new Button(this); button.setTag("underbutton"); button.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); button.setText("上のボタンちゃん"); layout.addView(button); // GLSurfaceViewを作る final GLSurfaceViewCust glSurfaceView = new GLSurfaceViewCust(this); glSurfaceView.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, // ↓ここを1.0にするのがミソ // これを指定しないとGLSurfaceView画面いっぱいに伸びて // 下のボタンが見えなくなる 1.0f)); layout.addView(glSurfaceView); // ボタンを設定する final Button button2 = new Button(this); button2.setTag("underbutton"); button2.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); button2.setText("下のボタンちゃん"); layout.addView(button2); } } // ここから下はGLSurfaceViewを使うために必要なだけで特別なことはしてない /////////////////////////////////////////////////////////////////////////// //GLSurfaceViewの拡張 class GLSurfaceViewCust extends GLSurfaceView { private GLRenderer mRenderer; // GLSurfaceViewに登録するレンダラー // コンストラクタ public GLSurfaceViewCust(Context context) { super(context); this.init(context); } // コンストラクタ public GLSurfaceViewCust(Context context, AttributeSet attrs) { super(context, attrs); this.init(context); } // 初期化する private void init(Context context) { // 描画オブジェクトの登録 this.mRenderer = new GLRenderer(); /////////////////////////////////////////////////////////////////////////// // OpenGL ES 2.0を指定する // このタイミングで無いとエラー出る this.setEGLContextClientVersion(2); /////////////////////////////////////////////////////////////////////////// this.setRenderer(this.mRenderer); } } /////////////////////////////////////////////////////////////////////////// //GLSurfaceView.Renderer拡張 class GLRenderer implements GLSurfaceView.Renderer { /** 毎フレーム呼ばれる */ public void onDrawFrame(GL10 gl) { /////////////////////////////////////////////////////////////////////////// // GL10が引数についてるけどGLES2.0を使うので無視してGLES20系の物を使う /////////////////////////////////////////////////////////////////////////// // 背景をクリア GLES20.glClearColor(0.3f, 0.3f, 0.3f, 1.0f); // 各バッファのクリア GLES20.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); } public void onSurfaceChanged(GL10 gl, int width, int height) { // TODO 自動生成されたメソッド・スタブ } public void onSurfaceCreated(GL10 gl, EGLConfig config) { // TODO 自動生成されたメソッド・スタブ } }
HTML5のcanvasを指定の背景色で塗りつぶすサンプル@tryHtml5Canvas00a AndroidのonTouchEventを使ってタッチパネルのタップの判定サンプル@trySc...