简体   繁体   English

如何在gridLayout中以编程方式设置列号和行号?

[英]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.这是我的 gridLayout 中的孩子之一。 I want to change its layout_column and layout_row from MainActivity.我想从 MainActivity 更改它的 layout_column 和 layout_row。

I am answering the question considering that you are working with the latest version of Android Studio.考虑到您正在使用最新版本的 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. 1.首先你不能直接从Main Activity修改布局。

2.You can change them by accessing the plentiful options available in the Pallete . 2.您可以通过访问Pallete 中提供的大量选项来更改它们。

3.Instead for layout_column and layout_row you can access the Linear Layout(horizontal and vertical) respectively. 3.对于 layout_column 和 layout_row,您可以分别访问线性布局(水平和垂直)。

4.Guideline Layout is also a secondary option available) 4.Guideline Layout也是一个可选的选项)

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). 5.如果您不想使用它们,那么您可以使用属性部分中存在的各种约束手动修改它们(希望您熟悉它们)。

What you are presenting here is just a part of the basic code present in the activity_main.xml portion of the project file.您在此处展示的只是项目文件的activity_main.xml 部分中存在的基本代码的一部分。 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.简而言之,如果要定义项目文件中的行数和列数,则 gridLayout 中存在两个属性。 These are columncount and rowcount present in the attributes.这些是属性中存在的columncountrowcount One can use these to define the numbers of columns and rows respectively.可以使用这些来分别定义列数和行数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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