![]() ![]() If you just left that as-is, you'd end up with a layout looking like this:Ĭlearly, there's more we can do here. Therefore, each of the simple cells is merely defined as follows: Instead of explicitly declaring a size (width & height) to a control with wrap_content or match_parent, the default is wrap_content for all children, and match_parent behaves the same as wrap_content as the sizing is controlled by different rules (which you can read all about in the GridLayout docs for creating more complex grid-aligned layouts).Įach cell will contain a single Button control with a text label. The child controls of the GridLayout control are defined a little differently than you might be used to. The following XML defines the GridLayout container we'll need: In this example, there are 4 columns.įinally, we want the View control in each cell (in this case, these are Button controls) to be centered and we want the whole layout to size itself to the content. Choosing horizontal means we have to limit the number of columns to get the automatic wrapping to the next row at the correct location. Horizontal orientation means the next cell is to the right, and also possibly wrapping around to the next row, starting on the left.įor this keypad, if we start on the forward slash cell (/), and use horizontal orientation, no cells need be skipped. That is, setting a vertical orientation means the next cell will be down a row from the current one and possibly moving right to the next column. ![]() GridLayout controls, like LinearLayout controls, can have horizontal and vertical orientations. ![]() But in Android 4.0, there's a more efficient control that suits our purposes: GridLayout. We'd likely resort to using a nested combination of LinearLayout controls-not the most efficient design. When designing a layout like this before GridLayout existed, we'd know that TableLayout use wouldn’t be feasible because of the row span. The following shows a rough sketch of the keypad we will build. You can download the sample source code we provide for review. We provide the full source code for the sample application discussed in this tutorial. Its flexibility, however, comes from the fact that it really helps to line up objects along the virtual grid lines created while building a view with GridLayout. For instance, cells can span rows, unlike with TableLayout. However, it's much more flexible than the TableLayout control. GridLayout () initially seems like it's a way to create tables much like TableLayout (). We'll create a simple numeric keypad using GridLayout to demonstrate a small taste of its power and elegance. In fact, it's a very useful new layout control. At first glance, you might wonder why the new GridLayout class even exists in Android 4.0 (aka Ice Cream Sandwich). ![]()
0 Comments
Leave a Reply. |