GridLayout Column width

前端 未结 3 2065
伪装坚强ぢ
伪装坚强ぢ 2021-02-11 12:56

I have 2 columns in my GridLayout. What I want to do is make those columns take up half of the width of the screen each and then have its child contents fill their

相关标签:
3条回答
  • 2021-02-11 13:17

    adding views dynamically in a grid Layout of 2 columns that take 50% of the available space:

    GridLayout gridLayout = new GridLayout();
    
    View view; //it can be any view
    
    GridLayout.LayoutParams param = new GridLayout.LayoutParams();
    
    param.columnSpec = GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f);
    
    param.width = 0;
    
    view.setLayoutParams(param);
    
    gridLayout.add(view);
    

    in static way (in .xml file).

    <android.support.v7.widget.GridLayout
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       app:alignmentMode="alignBounds"
       app:columnCount="2"
       app:columnOrderPreserved="false"
       app:orientation="horizontal"
       android:layout_margin="20dp"
       android:layout_marginBottom="30dp"
       android:padding="4dp"
       app:rowCount="2">
    
    <TextView
        android:layout_marginTop="2dp"
        android:id="@+id/edit_profile_username"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_column="0"
        app:layout_row="0"
        app:layout_gravity="fill"
        app:layout_columnWeight="1"
        android:text="@string/user_name" />
    
    <TextView
        android:layout_marginTop="2dp"
        android:id="@+id/edit_profile_first_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_column="1"
        app:layout_row="0"
        app:layout_gravity="fill"
        app:layout_columnWeight="1"
        android:text="@string/first_name" />
    
    <TextView
        android:layout_marginTop="2dp"
        android:id="@+id/edit_profile_middle_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_column="0"
        app:layout_gravity="fill"
        app:layout_columnWeight="1"
        app:layout_row="1"
        android:text="@string/middle_name" />
    
    <TextView
        android:layout_marginTop="2dp"
        android:id="@+id/edit_profile_last_name" 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_column="1"
        app:layout_gravity="fill"
        app:layout_columnWeight="1"
        app:layout_row="1"
        android:text="@string/last_name" />
    
    </android.support.v7.widget.GridLayout>
    
    0 讨论(0)
  • 2021-02-11 13:25

    For pre API 21, use support library:

    add

    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    

    to your dependencies.

    Then in your xml file:

    <android.support.v7.widget.GridLayout
                        xmlns:app="http://schemas.android.com/apk/res-auto"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:columnCount="2"
                        app:orientation="horizontal"
                        app:rowCount="1">
    
                        <TextView
                            android:text="1"
                            android:textStyle="bold"
                            app:layout_columnWeight="1"
                            />
    
                        <TextView
                            android:text="2"
                            android:textStyle="bold"
                            app:layout_columnWeight="1" />
    
    </android.support.v7.widget.GridLayout>
    

    Here note the use of "app" prefix and dont forget to add

    xmlns:app="http://schemas.android.com/apk/res-auto"
    

    to your xml file

    0 讨论(0)
  • 2021-02-11 13:26

    This code is available on pre API21 with support library!

    I have a simple piece of code to show 4 buttons in a gridLayout of 2 columns that take 50% of the available space: perhaps it can help

    <GridLayout
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="2"
        >
    
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_gravity="fill"
            android:layout_columnWeight="1"
            />
    
           <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_gravity="fill"
            android:layout_columnWeight="1"
            />
    
           <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_gravity="fill"
            android:layout_columnWeight="1"
            />
    
           <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_gravity="fill"
            android:layout_columnWeight="1"
            />
    
    
    
    </GridLayout>
    

    Solution is perhaps this :

    android:layout_gravity="fill"
    android:layout_columnWeight="1"
    
    0 讨论(0)
提交回复
热议问题