▼Androidメモ▼
リレイティブレイアウト


リレイティブレイアウトを利用するプログラムを作成する。



ソースコード
RelativeLayoutEx.java
package net.npaka.relativelayoutex;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;

//リレイティブレイアウト
public class RelativeLayoutEx extends Activity {
    private final static int WC=RelativeLayout.LayoutParams.WRAP_CONTENT;

    //アプリの初期化
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //レイアウトの生成
        RelativeLayout layout=new RelativeLayout(this);
        layout.setBackgroundColor(Color.WHITE);
        setContentView(layout);        

        //ボタン1の生成
        Button button1=new Button(this);
        button1.setId(1);
        button1.setText("(1)");
        button1.setLayoutParams(new RelativeLayout.LayoutParams(200,WC));
        layout.addView(button1);
        
        //ボタン2の生成
        Button button2=new Button(this);
        button2.setId(2);
        button2.setText("(2) 1の下");
        RelativeLayout.LayoutParams params2;
        params2=new RelativeLayout.LayoutParams(200,WC);
        params2.addRule(RelativeLayout.BELOW,1);//1の下
        button2.setLayoutParams(params2);
        layout.addView(button2);

        //ボタン3の生成
        Button button3=new Button(this);
        button3.setId(3);
        button3.setText("(3) 1の下 2の右");
        RelativeLayout.LayoutParams params3;
        params3=new RelativeLayout.LayoutParams(200,WC);
        params3.addRule(RelativeLayout.BELOW,1);//1の下
        params3.addRule(RelativeLayout.RIGHT_OF,2);//2の右
        button3.setLayoutParams(params3);
        layout.addView(button3);
        
        //ボタン4の生成
        Button button4=new Button(this);
        button4.setId(4);
        button4.setText("(4) 2の右 3の下");
        RelativeLayout.LayoutParams params4;
        params4=new RelativeLayout.LayoutParams(200,WC);
        params4.addRule(RelativeLayout.RIGHT_OF,2);//2の右
        params4.addRule(RelativeLayout.BELOW,3);//3の下
        button4.setLayoutParams(params4);
        layout.addView(button4);
    }
}



−戻る−