search

Home  >  Q&A  >  body text

android-studio - android gridlayout动态多行多列的问题

代码和效果图如上,请问

1,如何将按钮紧密连接在一起,像扫雷游戏那样?我设置了margin,都不行。

2,如何在点击其中一个按钮后,获取它在gridlayout的位置呢?代码如下,没有效果

/**

private View.OnClickListener clickListener = new View.OnClickListener() {

@Override
public void onClick(View v) {
Button btn = (Button)v;
GridLayout.LayoutParams params = (GridLayout.LayoutParams)btn.getLayoutParams();
GridLayout.Spec spec = params.rowSpec;

Log.d("AAA",params.rowSpec + "aa");

}

PHP中文网PHP中文网2771 days ago928

reply all(3)I'll reply

  • PHPz

    PHPz2017-04-18 09:06:11

    1. There is margin between buttons, which is caused by the default style of the system Button. The background image of Button's default style is an insetDrawable with white space around it. In order to solve the problem of white space, you can set a background for the Button yourself, so that the gap can be eliminated.

    2. GridLayout is different from GridView. It is only responsible for the placement of sub-controls and is not responsible for recording the location information of sub-controls and the distribution of click events. This requires you to monitor each sub-control individually.
      In order to obtain the position of the sub-control in GridLayout, it depends on whether your code is dynamically generated and not configured through xml files. You can bind your own position information to the control through View.setTag() when generating the sub-control. , obtain the location information through View.getTag() in the onClick() event.

    Hope it helps.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:06:11

    Just set the width of the item view to match_parnet

    reply
    0
  • PHPz

    PHPz2017-04-18 09:06:11

    Do you have to use GridLayout? You can use GridView or RecycleView instead. I think GridView is better

    reply
    0
  • Cancelreply