Proportional area usage with GridLayout

1

I'm having the following difficulty, after searching exhaustively in Google and not finding an answer to my doubt, I decided to ask here, I would like to put a TextView next to a Button, so that TextView uses 2/3 of useful area and the Button the other third remaining.

Currently the code looks like this:

<GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:columnCount="3"
        >

        <TextView
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Não possui uma conta?"
            android:background="#000"
            android:layout_height="40dp"
            android:id="@+id/textView5"
            android:layout_columnSpan="2"
            android:layout_column="0"
            android:gravity="start|center" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_height="40dp"
            android:text="CADASTRE-SE"
            android:id="@+id/button"
            android:layout_column="2"
            android:textStyle="bold"
            android:background="#d69312"
            android:gravity="center" />

</GridLayout>
    
asked by anonymous 09.06.2015 / 14:29

1 answer

1

Use a LinearLayout and use the android:layout_weight attribute to proportionally define the space that each View takes up, then assign that layout to the zero column of your grid indicating a span of 3

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:columnCount="3"
        >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="3"
        android:layout_column="0">

            <TextView
                android:id="@+id/textView5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Não possui uma conta?"
                android:textAppearance="?android:attr/textAppearanceSmall" 
                android:layout_weight="2"/>

            <Button
                android:id="@+id/button"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:background="#d69312"
                android:text="CADASTRE-SE"
                android:textStyle="bold" 
                android:layout_weight="1"/>

    </LinearLayout>
</GridLayout>

The sum of the values indicated in the android:layout_weight attribute corresponds to 100% of the total space. In the present case, 100% corresponds to 3.

So by specifying android:layout_weight="2" in TextView , we are saying that it should occupy 2/3 of the space.

By the same token, by saying android:layout_weight="1" in Button , we say that the space to occupy by it must be 1/3 of the total space.

Note: The use of the android:layout_weight attribute requires that, according to the LinearLayout orientation, android:layout_width or android:layout_height is set to "0dp" .

EDIT

From the API 21, the Gridlayout has the concept of weight , so with xml below, you get a result equal to that of xml .

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:columnCount="2">

        <TextView
            android:id="@+id/textView5"
            android:layout_gravity="fill_horizontal"
            android:text="Não possui uma conta?"
            android:textAppearance="?android:attr/textAppearanceSmall" 
            android:layout_column="0"
            android:layout_columnWeight="2"/>

        <Button
            android:id="@+id/button"
            style="?android:attr/buttonStyleSmall"
            android:layout_gravity="fill_horizontal"
            android:background="#d69312"
            android:text="CADASTRE-SE"
            android:textStyle="bold" 
            android:layout_column="1"
            android:layout_columnWeight="1"/>

</GridLayout>
    
09.06.2015 / 15:41