代码和效果图如上,请问
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");
}
PHPz2017-04-18 09:06:11
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.
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.
PHPz2017-04-18 09:06:11
Do you have to use GridLayout? You can use GridView or RecycleView instead. I think GridView is better