平均分布example——layoutItemsUsingGravity()


自动将各个item平均分布:

/
Figures out the layout for the menu items by equally distributing, and
adding any excess items equally to lower rows.

@param numRows The total number of rows for the menu view
@param numItems The total number of items (across all rows) contained in
the menu view
@return int[] Where the value of index i contains the number of items for row i
/
private void layoutItemsUsingGravity(int numRows, int numItems) {
int numBaseItemsPerRow = numItems / numRows;
int numLeftoverItems = numItems % numRows;
/

The bottom rows will each get a leftover item. Rows (indexed at 0)
that are >= this get a leftover item. Note: if there are 0 leftover
items, no rows will get them since this value will be greater than
the last row.
*/
int rowsThatGetALeftoverItem = numRows - numLeftoverItems;

int[] layout = mLayout;
for (int i = 0; i < numRows; i++) {
layout[i] = numBaseItemsPerRow;

// Fill the bottom rows with a leftover item each
if (i >= rowsThatGetALeftoverItem) {
layout[i]++;
}
}

mLayoutNumRows = numRows;
}