RelativeLayoutコードバージョン(xmlを使わないRelativeLayout)

記事内に広告が含まれていることがあります

場合によって、Buttonの色なんかを変えたくて
ググってみたけど。
クリックの有無での変化は
たくさんサンプルが転がっていたけど
それ以外はみつけられず。
で、RelativeLayoutをxmlを使わないで
コードで書いて
そこで条件文を使ってみることにした。
でも、RelativeLayoutのコードのみバージョンってーのも
サンプルがない。
仕方ないから、試行錯誤しながら、作ってみた。
下は真ん中の「1」を基準としているが
上はそれ以外で指定してみた。
一応、できたけど、これって、機種によって
崩れたりってこと、ないよね…。
初心者ゆえに、経験なし。自信なし。
だから、サンプルがネットにないことに、一抹の不安を感じる…。
$文系女子。だけどandroid。-RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);
Button button1 = new Button(this);
button1.setId(1);
button1.setText(“1”);
RelativeLayout.LayoutParams param1 = createParam(85, 85);
param1.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.addView(button1, param1);
Button button2 = new Button(this);
button2.setId(2);
button2.setText(“2”);
RelativeLayout.LayoutParams param2 = createParam(85, 85);
param2.addRule(RelativeLayout.CENTER_VERTICAL,1);
param2.addRule(RelativeLayout.LEFT_OF, 1);
relativeLayout.addView(button2, param2);
Button button3 = new Button(this);
button3.setId(3);
button3.setText(“3”);
RelativeLayout.LayoutParams param3 = createParam(85, 85);
param3.addRule(RelativeLayout.CENTER_VERTICAL,1);
param3.addRule(RelativeLayout.RIGHT_OF, 1);
relativeLayout.addView(button3, param3);
Button button4 = new Button(this);
button4.setId(4);
button4.setText(“4”);
RelativeLayout.LayoutParams param4 = createParam(85, 85);
param4.addRule(RelativeLayout.CENTER_HORIZONTAL,1);
param4.addRule(RelativeLayout.BELOW, 1);
relativeLayout.addView(button4, param4);
Button button5 = new Button(this);
button5.setId(5);
button5.setText(“5”);
RelativeLayout.LayoutParams param5 = createParam(85, 85);
param5.addRule(RelativeLayout.CENTER_HORIZONTAL,1);
param5.addRule(RelativeLayout.BELOW,1);
param5.addRule(RelativeLayout.RIGHT_OF, 1);
relativeLayout.addView(button5, param5);
Button button6 = new Button(this);
button6.setId(6);
button6.setText(“6”);
RelativeLayout.LayoutParams param6 = createParam(85, 85);
param6.addRule(RelativeLayout.CENTER_HORIZONTAL,1);
param6.addRule(RelativeLayout.BELOW,1);
param6.addRule(RelativeLayout.LEFT_OF,1 );
relativeLayout.addView(button6, param6);
Button button7 = new Button(this);
button7.setId(7);
button7.setText(“7”);
RelativeLayout.LayoutParams param7 = createParam(85, 85);
param7.addRule(RelativeLayout.CENTER_HORIZONTAL,1);
param7.addRule(RelativeLayout.ABOVE, 1);
relativeLayout.addView(button7, param7);
Button button8 = new Button(this);
button8.setId(8);
button8.setText(“8”);
RelativeLayout.LayoutParams param8 = createParam(85, 85);
param8.addRule(RelativeLayout.RIGHT_OF,7);
param8.addRule(RelativeLayout.ABOVE, 1);
relativeLayout.addView(button8, param8);
Button button9 = new Button(this);
button9.setId(9);
button9.setText(“9”);
RelativeLayout.LayoutParams param9 = createParam(85, 85);
param9.addRule(RelativeLayout.LEFT_OF,7);
param9.addRule(RelativeLayout.ABOVE, 2);
relativeLayout.addView(button9, param9);
}
private RelativeLayout.LayoutParams createParam(int w, int h){
return new RelativeLayout.LayoutParams(w, h);
}

※便利グッズ、新商品情報を継続的に発信しているのでTwitterをフォローして頂けると嬉しいです! @dekoppon
※ライターとして執筆担当したサイトと同じ画像を使用していることがあります。許可済
プログラム
mamaroid

コメント