我正在以编程方式构建一个 RelativeLayout,它有 3 个按钮 - 一个位于屏幕中央,一个位于其上方,另一个位于其下方。
如何填充/间隔它们以增加按钮之间的空白空间?它们与我当前的代码几乎紧挨着放置,这可能会导致无意的误按。
private RelativeLayout myLayout;
private void addButtons() {
// Construct the 2 player game button.
Button start2pGameButton = new Button(this);
start2pGameButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
// Process the button tap and start a 2 player game.
startFrozenBubble(2);
}
});
start2pGameButton.setText("Player vs. CPU");
start2pGameButton.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);
start2pGameButton.setWidth((int) (start2pGameButton.getTextSize() * 10));
start2pGameButton.setHorizontalFadingEdgeEnabled(true);
start2pGameButton.setFadingEdgeLength(5);
start2pGameButton.setShadowLayer(5, 5, 5, R.color.black);
start2pGameButton.setId(BTN2_ID);
LayoutParams myParams1 = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
myParams1.addRule(RelativeLayout.CENTER_HORIZONTAL);
myParams1.addRule(RelativeLayout.CENTER_VERTICAL);
// Add view to layout.
myLayout.addView(start2pGameButton, myParams1);
// Construct the 1 player game button.
Button start1pGameButton = new Button(this);
start1pGameButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
// Process the button tap and start/resume a 1 player game.
startFrozenBubble(1);
}
});
start1pGameButton.setText("Puzzle");
start1pGameButton.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);
start1pGameButton.setWidth((int) (start1pGameButton.getTextSize() * 10));
start1pGameButton.setHorizontalFadingEdgeEnabled(true);
start1pGameButton.setFadingEdgeLength(5);
start1pGameButton.setShadowLayer(5, 5, 5, R.color.black);
start1pGameButton.setId(BTN1_ID);
LayoutParams myParams2 = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
myParams2.addRule(RelativeLayout.CENTER_HORIZONTAL);
myParams2.addRule(RelativeLayout.ABOVE, start2pGameButton.getId());
// Add view to layout.
myLayout.addView(start1pGameButton, myParams2);
// Construct the options button.
Button optionsButton = new Button(this);
optionsButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
// Process the button tap and start the preferences activity.
startPreferencesScreen();
}
});
optionsButton.setText("Options");
optionsButton.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);
optionsButton.setWidth((int) (optionsButton.getTextSize() * 10));
optionsButton.setHorizontalFadingEdgeEnabled(true);
optionsButton.setFadingEdgeLength(5);
optionsButton.setShadowLayer(5, 5, 5, R.color.black);
optionsButton.setId(BTN3_ID);
LayoutParams myParams3 = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
myParams3.addRule(RelativeLayout.CENTER_HORIZONTAL);
myParams3.addRule(RelativeLayout.BELOW, start2pGameButton.getId());
// Add view to layout.
myLayout.addView(optionsButton, myParams3);
}