intA
intA

Reputation: 2701

My gridlayout is not inflating as expected

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.package.name.MainActivity">

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="1"
            android:layout_row="2"
            android:src="@color/colorAccent"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="3"
            android:layout_row="2"
            android:src="@color/colorPrimary"/>

    </GridLayout>

</LinearLayout>

This is my layout file. I was expecting a 4x4 grid with the accent color in column 1, row 2 and the primary color in column 3, row 2, the rest blank. But instead I just get the whole gridlayout filled with the accent color, there isn't even a cell with the primary color. Why is this? How do I achieve what I want?

Thanks!

Upvotes: 1

Views: 72

Answers (1)

Cheticamp
Cheticamp

Reputation: 62841

Your first ImageView has a width and height of match_parent, so it is filling the entire layout with itself. You can see something like you want to see by just defining a width and height to each ImageView, say 100dp. That's not what you want, but it can get you started. Also, take a look at "Excess Space Distribution" at https://developer.android.com/reference/android/widget/GridLayout.html.

Upvotes: 1

Related Questions