SparkyNZ
SparkyNZ

Reputation: 6676

Android GridLayout - XML - columns not in proportion

I am trying to create a button layout that looks like this on the first row:

[  1 ][2][3]

Button 1 should occupy 2 columns and be twice the width of buttons 2 and 3. Buttons 2 and 3 should be the same width (1 column wide).

However, what I am seeing is this:

[1][2][       3       ]

Can somebody please tell me why button 3 completely fills the rest of the screen and why the buttons don't seem to be obeying the layout_column_span rules?

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_margin="0dp"
    android:adjustViewBounds="true"
    android:alignmentMode="alignMargins"
    android:clipToPadding="false"
    android:columnCount="4"
    android:fitsSystemWindows="false"
    android:layoutMode="clipBounds"
    android:orientation="horizontal"
    android:padding="0dp"
    android:rowCount="6"
    android:useDefaultMargins="true" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="fill"
            android:layout_weight="1"
            android:layout_margin="0dp"
            android:background="#ff0000"
            android:layout_row="0"
            android:layout_column="0"
            android:layout_columnSpan="2"
            android:layout_rowSpan="2"            
            android:text="1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="fill"
            android:layout_weight="1"
            android:layout_margin="0dp"
            android:background="#ffff00"
            android:layout_row="0"
            android:layout_column="2"
            android:layout_columnSpan="1"
            android:layout_rowSpan="2"            
            android:text="2" />

       <Button
           android:id="@+id/button3"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_column="3"
           android:layout_columnSpan="1"
           android:layout_gravity="fill"
           android:layout_margin="0dp"
           android:layout_row="0"
           android:layout_rowSpan="2"
           android:layout_weight="1"
           android:background="#00ff00"
           android:text="3" />


</GridLayout>

Upvotes: 0

Views: 653

Answers (2)

joao2fast4u
joao2fast4u

Reputation: 6892

According to this post, there are some limitations when it comes to arranging space in a non-trivial manner on a GridLayout . You can try another approach or you can use a LinearLayout for each row, so you can control weights for its children Views.

Here is an example for your case:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="1" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="4" >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="2"
        android:singleLine="true"
        android:text="Button 1" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:singleLine="true"
        android:text="Button 2" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:singleLine="true"
        android:text="Button 3" />
</LinearLayout>

Hope to have helped you.

Upvotes: 0

Palak
Palak

Reputation: 574

 Check this may help you
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/favorites_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:columnCount="1"
android:rowCount="2"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_column="0"
    android:layout_gravity="left|top"
    android:layout_row="0"
    android:orientation="horizontal"
    android:weightSum="4" >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
         android:text="Cell 1" 
        android:textSize="14dip" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cell 2" />

    <Button
         android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cell 3" 
        android:textSize="14dip" />
</LinearLayout>

Upvotes: 1

Related Questions