0

我正在以编程方式构建一个 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);
}
4

3 回答 3

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);
于 2013-07-10T20:43:31.600 回答
0

您可以使用setMargins函数来设置视图的边距:

myParams1.setMargins(left, top, right, bottom);
于 2013-07-10T20:42:05.510 回答
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

希望这会有所帮助,问候!

于 2013-07-10T20:37:42.557 回答