Dark.Rider
Dark.Rider

Reputation: 391

GridLayout not filling rows evenly (Android)

I'm trying to get an evenly filled grid using GridLayout (API 21). Filling in the horizontal direction works fine using layout_columnWeight attribute. The same with layout_rowWeight attribute fails (see screenshot) enter image description here. I am pretty clueless.. It seems like the both attributes don't work in the same way.

Also even more simplified layouts show the same behavior (1 row x 2 columns works, 2 rows x 1 column fails). Also explicitly adding of layout_row and layout_column attributes don't change anything.

Please don't answer "use linearLayout". I want to get it working with the GridLayout.

<FrameLayout 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:background="#0099cc" 
    tools:context=".Locomotion">

    <GridLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnCount="2"
        android:rowCount="2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="north west"
            android:id="@+id/textViewNW"
            android:layout_rowSpan="1"
            android:layout_rowWeight="1"
            android:layout_columnSpan="1"
            android:layout_columnWeight="1"
            android:layout_gravity="center|fill"
            android:background="#fe4141" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="north east"
            android:id="@+id/textViewNE"
            android:layout_rowSpan="1"
            android:layout_rowWeight="1"
            android:layout_columnSpan="1"
            android:layout_columnWeight="1"
            android:layout_gravity="center|fill"
            android:background="#51f328" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="south west"
            android:id="@+id/textViewSW"
            android:layout_rowSpan="1"
            android:layout_rowWeight="1"
            android:layout_columnSpan="1"
            android:layout_columnWeight="1"
            android:layout_gravity="center|fill"
            android:background="#fefe00" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="south east"
            android:id="@+id/textViewSE"
            android:layout_rowSpan="1"
            android:layout_rowWeight="1"
            android:layout_columnSpan="1"
            android:layout_columnWeight="1"
            android:layout_gravity="center|fill"
            android:background="#0080f0" />

    </GridLayout>
</FrameLayout>

Thanks in advance for any hint!

Thomas

Upvotes: 0

Views: 711

Answers (1)

BigDru
BigDru

Reputation: 161

This was difficult. Had to get creative to solve it. Basically my solution was to nest GridLayouts for each col with a master GridLayout for 1 row. Also each col GridLayout must be in orientation="verticle" with width/height = "wrap_content"

Result:

enter image description here

Here's the code:

<FrameLayout 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:background="#0099cc"
    tools:context=".Locomotion">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="2"
        android:rowCount="1">

        <GridLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:columnCount="1"
            android:rowCount="2"
            android:layout_column="0"
            android:layout_columnSpan="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textViewNW"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_columnSpan="1"
                android:layout_columnWeight="1"
                android:layout_gravity="fill"
                android:layout_row="0"
                android:layout_rowSpan="1"
                android:layout_rowWeight="1"
                android:background="#fe4141"
                android:text="north west"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/textViewSW"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_columnSpan="1"
                android:layout_columnWeight="1"
                android:layout_gravity="fill"
                android:layout_row="1"
                android:layout_rowSpan="1"
                android:layout_rowWeight="1"
                android:background="#fefe00"
                android:text="south west"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </GridLayout>

        <GridLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:columnCount="1"
            android:rowCount="2"
            android:layout_column="1"
            android:layout_columnSpan="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textViewNE"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_columnSpan="1"
                android:layout_columnWeight="1"
                android:layout_gravity="fill"
                android:layout_row="0"
                android:layout_rowSpan="1"
                android:layout_rowWeight="1"
                android:background="#51f328"
                android:text="north east"
                android:textAppearance="?android:attr/textAppearanceLarge" />


            <TextView
                android:id="@+id/textViewSE"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_columnSpan="1"
                android:layout_columnWeight="1"
                android:layout_gravity="fill"
                android:layout_row="1"
                android:layout_rowSpan="1"
                android:layout_rowWeight="1"
                android:background="#0080f0"
                android:text="south east"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </GridLayout>
    </GridLayout>
</FrameLayout>

Upvotes: 1

Related Questions