0

I have a GridView in a vertical LinearLayout, that I want to have take up all remaining vertical space. The grid will always have 5 columns and 7 rows. The grid items are just a TextView. I want the grid items to expand proportionally (so that each item is one fifth of the width and one seventh of the height) to fill remaining space on any device. I tried setting layout weights, but that didn't do anything. How can I make it fill the space?

Here's my activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  android:orientation="vertical">

  <TextView
    android:text="@string/zero"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tv0"
    android:gravity="end"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    android:paddingEnd="8dp"
    android:paddingStart="8dp"
    android:textColor="#e4e4e4"
    android:background="@color/colorPrimaryDark"/>

  <!-- A few more TextViews -->

  <GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:horizontalSpacing="0dp"
    android:verticalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:numColumns="5"/>
</LinearLayout>

And here's the layout for the grid item:

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

  <TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/colorPrimary"
    android:paddingBottom="15dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="15dp"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
</FrameLayout>
kwiqsilver
  • 1,017
  • 2
  • 11
  • 24

1 Answers1

0

I didn't understand exacly what you meant by "proportionally", but to make the GridView "fill the space" in your example you don't need to set layout weights:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text" />

    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:horizontalSpacing="0dp"
        android:numColumns="5"
        android:stretchMode="columnWidth"
        android:verticalSpacing="0dp" />

</LinearLayout>

Generates the layout below:

enter image description here

  • 1
    That's what I see in the design view, but when I run it on my Nexus 6P, I see a big white gap at the bottom. – kwiqsilver May 24 '16 at 07:51
  • That's true. I belive [this](http://stackoverflow.com/a/11049803/2776632) answer describes the arrangement you're looking for. Take a look. – Diego dos Santos May 24 '16 at 18:36