我正在做一個小應用程序,因爲我在學習Android這些天,我想出了一個小問題,我做了一個RelativeLayout(在java文件中,而不是xml文件),然後引入按鈕在一個相對於另一個。Center RelativeLayout或它的內容
最終佈局其是這樣的:
我做的是我創建中間的一排按鈕,然後將所有相對於他的其他按鈕。我想要做的是將RelativeLayout放置在視圖中,這樣我的電路板就居中了,我可以在屏幕上放置一些文本或其他東西。我曾考慮過在RelativeLayout之前創建一個LinearLayout,然後設置一些類似margin的內容或將RelativeLAyout引入另一個佈局來居中,但似乎不工作,您建議我如何實現該目標而不必更改我製作電路板的方式?這裏是製作電路板的代碼:
private RelativeLayout makeBoard(){
int papi = 4;
int grande =3;
int peque = 1;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(wrap_content,wrap_content);
RelativeLayout parent = new RelativeLayout(this);
parent.setLayoutParams(params);
papi = makeRow(parent,papi,peque);
papi = makeRow(parent,papi,peque);
papi = makeRow(parent,papi,grande);
papi = makeRow(parent,papi,grande);
papi = makeRow(parent,papi,grande);
papi = makeRow(parent,papi,peque);
papi = makeRow(parent,papi,peque);
return parent;
}
private int makeRow (RelativeLayout parent, int papasote,int colDer)
{
int index = papasote+10;
if (papasote==4)
{
createButtonSinMas (parent, index);
}
else
{
createButtonJustoAbajo(parent, index, papasote);
}
for (int i=0;i<colDer;i++)
{
createButtonAlaDeresha(parent, index+i+1, index+i);
createButtonAlaIskierda(parent, index-i-1, index-i);
}
return index;
}
private int createButtonSinMas (RelativeLayout parent, int id)
{
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(SIZE, SIZE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
CCCButton button = new CCCButton(this);
button.setLayoutParams(params);
return basura(parent,id,params, button);
}
private int createButtonJustoAbajo (RelativeLayout parent, int id, int papasote)
{
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(SIZE, SIZE);
params.addRule(RelativeLayout.BELOW, papasote);
params.addRule(RelativeLayout.ALIGN_RIGHT, papasote);
CCCButton button = new CCCButton(this);
return basura(parent,id,params, button);
}
private int createButtonAlaDeresha (RelativeLayout parent, int id, int papasote)
{
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(SIZE, SIZE);
params.addRule(RelativeLayout.RIGHT_OF, papasote);
params.addRule(RelativeLayout.ALIGN_BASELINE,papasote);
CCCButton button = new CCCButton(this);
return basura(parent,id,params, button);
}
private int createButtonAlaIskierda (RelativeLayout parent, int id, int papasote)
{
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(SIZE, SIZE);
params.addRule(RelativeLayout.LEFT_OF, papasote);
params.addRule(RelativeLayout.ALIGN_BASELINE,papasote);
CCCButton button = new CCCButton(this);
return basura(parent,id,params, button);
}
private int basura(RelativeLayout parent,int id,RelativeLayout.LayoutParams params, CCCButton button)
{
button.setId(id);
button.setLayoutParams(params);
button.setOnClickListener(this);
actualizarBoton(button);
parent.addView(button);
return button.getId();
}
然後我只是在我的主板= makeBoard();板是一個RelativeLayout變量。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
game = new Game();
board = makeBoard();
setContentView(board);
}
在此先感謝球員
您如何將這個「board」添加到屏幕/主佈局? 'setContentView'什麼的?發佈該部分.. –
是的,我只是在onCreate做setContentView(板),添加了onCreate代碼 – Khiven