In my layout I have only eight cells.
I want the cell divide all the space available on the screen. Is this possible?
This is my gridview layout:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dataGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10px"
android:verticalSpacing="10px"
android:horizontalSpacing="10px"
android:numColumns="2"
android:gravity="fill" />
And this is the item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dip" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#E00000"
android:textStyle="bold"
android:gravity="left"
android:textSize="16dip"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
android:textStyle="normal"
android:gravity="right"
android:textSize="12dip"/>
</LinearLayout>
</TableRow>
</LinearLayout>
Unfortunately, GridView
is a tricky layout to manipulate to get internal views to fit a specific size like you are attempting. I think it is more appropriate to think of GridView
as an advanced ListView
for showing variable-length, scrollable lists like thumbnails in a photo album, as opposed to a "grid." I started down this route when I originally wanted to make a fixed sized grid for a game, but it is really not an appropriate layout for that at all.
Since your grid is always 8 cells, might I suggest using nested LinearLayout
s (or if you don't want all the cells exactly the same size, the more complex TableLayout
/ TableRow
combo) instead:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1.0" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.5"
android:orientation="horizontal"
android:weightSum="1.0" >
<!-- Drop in 4 items here with layout_weight of 0.25 -->
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.5"
android:orientation="horizontal"
android:weightSum="1.0" >
<!-- Drop in 4 items here with layout weight of 0.25 -->
</LinearLayout >
</LinearLayout >
This would make a fixed 2x4 grid that always exactly fills the screen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With