Java 如何设置按钮的间距
我正在以编程方式构建RelativeLayout,它有3个按钮——一个在屏幕中央,一个在上面,另一个在下面 如何填充/隔开按钮以增加按钮之间的空白?它们与我当前的代码几乎紧靠在一起,这可能会导致无意中误按Java 如何设置按钮的间距,java,android,android-relativelayout,Java,Android,Android Relativelayout,我正在以编程方式构建RelativeLayout,它有3个按钮——一个在屏幕中央,一个在上面,另一个在下面 如何填充/隔开按钮以增加按钮之间的空白?它们与我当前的代码几乎紧靠在一起,这可能会导致无意中误按 private RelativeLayout myLayout; private void addButtons() { // Construct the 2 player game button. Button start2pGameButton = new Button(this
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);
}
最值得推荐的方法是使用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
希望这有帮助,问候 您可以使用
设置页边距
功能设置视图的页边距:
myParams1.setMargins(left, top, right, bottom);
希望这对你有用
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);
为每个按钮设置边距布局权重是根据不同屏幕的未知大小在布局中系统地分隔按钮的一种方法。并按照建议将其与利润率相结合。通常是那些喜欢用XML尽可能多地完成UI工作的人。是的,XML使这变得非常简单。这只是安卓系统的问题:layout_marginBottom=“X”。我不确定为什么在用java编写时按钮没有.setMargin方法,至少我没有看到。有一个.setPadding方法可以尝试,但这可能不会在按钮之间留出空间,而只是在按钮内部留出更多空间;myParams1.bottomMargin=15;这就成功了-谢谢tyczj!如果不起作用,请尝试RelativeLayout.LayoutParams params=新建RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_父项,RelativeLayout.LayoutParams.WRAP_内容);