在 RelativeLayout 裡有兩個鋼琴鍵,我使用java代碼把這兩個按鈕在 RelativeLayout 中一個挨一個對齊。
代碼如下:
wk[0] = new Button(this);
wk[1] = new Button(this);
wk[0].setId(0);
wk[1].setId(1);
wkParams[0] = new RelativeLayout.LayoutParams(wkw,wkh);
wkParams[1] = new RelativeLayout.LayoutParams(wkw,wkh);
wkParams[0].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
wkParams[1].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
wkParams[1].addRule(RIGHT_OF,wk[0].getId());
relativeLayout.addView(wk[0],wkParams[0]);
relativeLayout.addView(wk[1],wkParams[1]);
但是現在獲得的是wk[0] 上面的wk[1] ,我不能看見 wk[0]。
改成這樣試試看:
wk[0] = new Button(this);
wk[1] = new Button(this);
wk[0].setId(0);
wk[1].setId(1);
wkParams[0] = new RelativeLayout.LayoutParams(wkw,wkh);
wkParams[1] = new RelativeLayout.LayoutParams(wkw,wkh);
wkParams[0].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
wk[0].setLayoutParams(wkParams[0]);
wkParams[1].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
wkParams[1].addRule(RIGHT_OF,wk[0].getId());
wk[1].setLayoutParams(wkParams[1]);
relativeLayout.addView(wk[0]);
relativeLayout.addView(wk[1]);