0

私はプログラムで RelativeLayout を構築しています。3 つのボタンがあります。1 つは画面の中央、もう 1 つはその上、もう 1 つはその下にあります。

ボタン間の空きスペースを増やすためにそれらをパディング/スペースするにはどうすればよいですか? 私の現在のコードでは、それらはほぼ隣り合って配置されているため、不注意による誤押しにつながる可能性があります。

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

これを行う最も推奨される方法は、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 に答える
0

関数を使用setMarginsしてビューの余白を設定できます。

myParams1.setMargins(left, top, right, bottom);
于 2013-07-10T20:42:05.510 に答える