The solution is simpler than it seems:
First: You should check if the LinearLayout
that you are involving her GridView
is with android:orientation="horizontal"
, If it’s not, you should put it on.
Ps: If you’re not wrapping her in a LinearLayout
(that would be out of standard) you must involve.
According to: You can only with a single attribute solve your problem, which would be the android:layout_weight="1"
You might ask yourself "But how did it get 20% the size of each column if you didn’t even set anything" - and then I answer you: Putting Weight 1 it automatically distributes in 5 items per "row" or horizontal we will always have 5 columns, and if you see:
(10/5itens) = 2 ----> 2 = 20% ----> 10 = 100%
Thus remaining:
<GridView android:id="@+id/grid1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:padding="5dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:numColumns="5"
android:columnWidth="120dp"
android:gravity="center"/>
Upshot:
Obs: Note that I put Width 0, because when we work with Weight, we should set the orientation size to 0, that is if the parent Linearlayout orientation was as vertical, the Weight would be applied vertically, then the Height would be 0.
Your layout is at 100% height as well as the
gridview
? And what you need is to set the columns to 20% height, that’s it?– Zuul
Not what I want is 20% width for each column
– anovaesneto
I assume what you’re looking for is gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH) since you have a fixed number of columns, you only need to indicate that they should occupy the entire width by the available size.
– Zuul