Exemplo n.º 1
0
        // method to create the grid layout
        private void makeTilesMethod()
        {
            // the grid is 4 x 4 -> the width of a tile is a quarter of the width of the grid
            tileWidth = gameViewWidth / 4;

            // create a counter to keep in mind the number of the tile
            int counter = 1;

            // initialize the lists for storing the coordinates of the tiles
            tilesList       = new ArrayList();
            coordinatesList = new ArrayList();

            // we need to create all 16 tiles, they being placed on 4 rows and 4 columns
            for (int row = 0; row < 4; ++row)
            {
                for (int column = 0; column < 4; ++column)
                {
                    // we want to make a tile and add it in our main layout
                    MyTextView textTile = new MyTextView(this);

                    // we want to put a tile on the row 'row' and column 'column'
                    GridLayout.Spec rowSpec = GridLayout.InvokeSpec(row);
                    GridLayout.Spec colSpec = GridLayout.InvokeSpec(column);

                    GridLayout.LayoutParams tileLayoutParams = new GridLayout.LayoutParams(rowSpec, colSpec);

                    // print the number of the tile on it
                    textTile.Text = counter.ToString();
                    textTile.SetTextColor(Color.Black);
                    textTile.TextSize = 40;
                    textTile.Gravity  = GravityFlags.Center; // put the text in the center of the tile

                    // set the width and height of the tile
                    tileLayoutParams.Width  = tileWidth - 10; // (-10) for visibility
                    tileLayoutParams.Height = tileWidth - 10; // (-10) for visibility
                    tileLayoutParams.SetMargins(5, 5, 5, 5);  // for making a nice looking contour


                    // make the changes in the actual textTile
                    textTile.LayoutParameters = tileLayoutParams;
                    textTile.SetBackgroundColor(Color.Green);

                    // keep the coordinates of a tile (as a point in space, where the X coordinate is the column and Y coordiante is the row)
                    Point thisLocation = new Point(column, row);
                    // add the coordiante of this point in the coordinatesList
                    coordinatesList.Add(thisLocation);
                    // add the tile in the list, to use it later
                    tilesList.Add(textTile);

                    // remember the position if the tile
                    textTile.xPos = thisLocation.X;
                    textTile.yPos = thisLocation.Y;

                    // assign a method to execute when we toch the button
                    textTile.Touch += TextTile_Touch;

                    // add the tile in the main layout
                    mainLayout.AddView(textTile);

                    // increase the counter to the next number
                    counter = counter + 1;
                }
            }

            // remove the 16th tile -> tilesList start from the position 0 (not 1 as expected), so the 16th element is on the position 15
            mainLayout.RemoveView((MyTextView)tilesList[15]);
            // remove the 16th tile also from our list
            tilesList.RemoveAt(15);
        }