简体   繁体   中英

How to set column and row number programmatically in gridLayout?

        <GridLayout
          <ImageView
            android:id="@+id/freeParking"
            android:layout_width="52dp"
            android:layout_height="58dp"
            android:background="#57979F"
            app:layout_column="0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_row="0"
            app:srcCompat="@drawable/ic_launcher_foreground" />

This is one of the child in my gridLayout. I want to change its layout_column and layout_row from MainActivity.

I am answering the question considering that you are working with the latest version of Android Studio.

Programmatically you can define the define the number of rows and columns as:

android:rowCount="number of rows" android:columnCount="number of columns"

There are a few points you must always keep in mind:

1.First of all you can't directly modify the layout from Main Activity.

2.You can change them by accessing the plentiful options available in the Pallete .

3.Instead for layout_column and layout_row you can access the Linear Layout(horizontal and vertical) respectively.

4.Guideline Layout is also a secondary option available)

5.If you don't wanna use them, then you can manually modify them using the various constraints present in the attributes section (hoping you are familiar with them).

What you are presenting here is just a part of the basic code present in the activity_main.xml portion of the project file. This portion of code has no area that can modify your desired layouts.

In short if you want to define the number of rows and columns in the project file, then there are two attributes present in gridLayout. These are columncount and rowcount present in the attributes. One can use these to define the numbers of columns and rows respectively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM