java - Creating a simple GridLayout not working -


i'm trying create simple grid layout in android programatically. have number of rows , columns, , color each square differently. have tried seems color every square value of last view (i believe), i'm not quite sure why. gridlayout doesn't seem have clear documentation. here's code:

public static final int game_board_columns = 8; public static final int game_board_rows = 8;  @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     view view = inflater.inflate(r.layout.fragment_game_board, container, false);     creategameboard(view);      return view; }  private void creategameboard(view view) {     gridlayout grid = (gridlayout)view.findviewbyid(r.id.grid_layout_game_board);     grid.setrowcount(game_board_rows);     grid.setcolumncount(game_board_columns);      (int = 0; < game_board_rows; i++) {         (int j = 0; j < game_board_columns; j++) {             gridlayout.layoutparams params =                  new gridlayout.layoutparams(gridlayout.spec(i), gridlayout.spec(j));              view square = new view(getactivity());             int color = color.rgb(                     (int) ((255.0f / (float) game_board_rows) * i),                     (int) ((255.0f / (float) game_board_columns) * j),                     100);             square.setbackgroundcolor(color);              grid.addview(square, params);         }     } } 

and gameboard layout simple:

<?xml version="1.0" encoding="utf-8"?> <gridlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/grid_layout_game_board"     android:layout_width="match_parent"     android:layout_height="match_parent">  </gridlayout> 

when run this, screen blue, can't quite figure out why, while debugging color different every iteration, i'm guessing it's not adding views gridlayout correctly. had experience in how populate gridlayout programmatically?

you might not need specify layoutparams. gridlayout should add cells sequentially provide them (using grid.addview(...)).

another possible issue square view object. should make sure view object returned has width , height set , none of children in view (if any) have widths or heights specified might fill screen.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -