我正在以编程方式构建一个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);
}
答案 0 :(得分:1)
希望这对您有用
Button btn1 = new Button(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); // width, height
params.setMargins(top, left, bottom, right); // dp values in integers
relativeLayout.addView(btn1, params);
答案 1 :(得分:0)
最值得推荐的方法是使用XML文件正确地扩展视图布局并使用边距,因为您以编程方式执行此操作,您需要做的是使用以下代码:
//For the button on top
MarginLayoutParams mlp = (MarginLayoutParams)yourButton.getLayoutParams();
mlp.bottomMargin = xxValue;
btn.setLayoutParams(mlp);
//And the same for the one on the bottom but using mlp.topMargin
希望这有帮助,尊重!
答案 2 :(得分:0)
您可以使用setMargins
功能设置视图的边距:
myParams1.setMargins(left, top, right, bottom);