Android 动态网格布局
我在另一节课上犯了一个错误,这就是为什么它不起作用。下面的代码似乎是正确的 我正在尝试创建一个动态网格布局。在另一个类(不是这个类)中,我有一个方法来设计gridlayout的行和列。在下面的类中,我向GridLayout添加了一些按钮:Android 动态网格布局,android,Android,我在另一节课上犯了一个错误,这就是为什么它不起作用。下面的代码似乎是正确的 我正在尝试创建一个动态网格布局。在另一个类(不是这个类)中,我有一个方法来设计gridlayout的行和列。在下面的类中,我向GridLayout添加了一些按钮: int buttons= 6;//the number of bottons i have to put in GridLayout int buttonsForEveryRow = 3; // buttons i can put inside every s
int buttons= 6;//the number of bottons i have to put in GridLayout
int buttonsForEveryRow = 3; // buttons i can put inside every single row
int buttonsForEveryRowAlreadyAddedInTheRow =0; // count the buttons added in a single rows
int columnIndex=0; //cols index to which i add the button
int rowIndex=0; //row index to which i add the button
for(int i=0; i < buttons;i++){
/*if numeroBottoniPerRigaInseriti equals numeroBottoniPerRiga i have to put the other buttons in a new row*/
if(buttonsForEveryRowAlreadyAddedInTheRow ==buttonsForEveryRow ){
rowIndex++; //here i increase the row index
buttonsForEveryRowAlreadyAddedInTheRow =0;
columnIndex=0;
}
Spec row = GridLayout.spec(rowIndex, 1);
Spec colspan = GridLayout.spec(columnIndex, 1);
GridLayout.LayoutParams gridLayoutParam = new GridLayout.LayoutParams(row, colspan);
gridLayout.addView(button_to_add,gridLayoutParam);
buttonsForEveryRowAlreadyAddedInTheRow ++;
columnIndex++;
int按钮=6//我必须在GridLayout中输入的Botton数
int buttonsForEveryRow=3;//我可以把每一行的按钮都放进去
int按钮永久性Y轴已添加到以下位置=0;//计算添加在一行中的按钮数
int columnIndex=0//添加按钮的cols索引
int rowIndex=0//添加按钮的行索引
对于(int i=0;i<按钮;i++){
/*如果numeroBottoniPerRigaInseriti等于numeroBottoniPerRiga,我必须将其他按钮放在新行中*/
if(按钮永久Y轴已添加到下面==按钮永久Y轴){
rowIndex++;//这里我增加了行索引
按钮ForeveryRowalReadedInTheRow=0;
柱状指数=0;
}
Spec row=GridLayout.Spec(rowIndex,1);
Spec colspan=GridLayout.Spec(columnIndex,1);
GridLayout.LayoutParams gridLayoutParam=新的GridLayout.LayoutParams(行、列);
addView(按钮添加到按钮添加,gridLayoutParam);
按钮永久旋转已添加到箭头++;
columnIndex++;
在下图中,您可以看到我得到的结果:按钮3和6缺失。恐怕我没有正确使用GridLayout.spec
使用GridLayout时有一些限制,以下引用摘自 “GridLayout不支持重量原则,因为 定义为重量。通常,不可能 将GridLayout配置为在非平凡空间中分配多余空间 多行或多列之间的比例…用于完全控制 在行或列中过多的空间分布;使用线性布局 用于保存关联单元格组中的组件的子视图。“ 下面是一个使用LinearLayout子视图的小示例。(我使用了占用未使用区域的空间视图,并将按钮按到所需位置。)
使用下面的代码,您可以使用列跨度和行跨度将图像视图动态添加到网格布局中
gridLayout = (GridLayout) findViewById(R.id.gridview);
gridLayout.removeAllViews();
int total = 10;
int column = 3;
int row = total / column;
gridLayout.setColumnCount(column);
gridLayout.setRowCount(row + 1);
for (int i = 0, c = 0, r = 0; i < total; i++, c++) {
if (c == column) {
c = 0;
r++;
}
ImageView oImageView = new ImageView(this);
oImageView.setImageResource(R.drawable.ic_launcher);
oImageView.setLayoutParams(new LayoutParams(100, 100));
Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
Spec colspan = GridLayout.spec(GridLayout.UNDEFINED, 1);
if (r == 0 && c == 0) {
Log.e("", "spec");
colspan = GridLayout.spec(GridLayout.UNDEFINED, 2);
rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 2);
}
GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
rowSpan, colspan);
gridLayout.addView(oImageView, gridParam);
}
gridLayout=(gridLayout)findViewById(R.id.gridview);
gridLayout.removeAllViews();
整数合计=10;
int列=3;
int行=总计/列;
gridLayout.setColumnCount(列);
gridLayout.setRowCount(行+1);
对于(int i=0,c=0,r=0;i
你的意思是什么?你的意思是我必须在layout.xml文件中设置列数和行数?没有通过codeaaaa设置,我是在另一个类中设置的。我使用了gridLayout.setColumnCount
和gridLayout.setRowCount
和getRowCount
和我看到行和列都是正确的。可能问题是我没有将我的按钮正确添加到GridLayout。Yoh在另一个类中出错,这就是它不起作用的原因。无论如何,谢谢。我的问题是我的代码没有显示按钮3和按钮6。我对spaceOH没有问题。这就是为什么它在另一个类中出错的原因不起作用。无论如何,谢谢你需要在这里提供你的GridLayout定义。我们无法猜测到底是什么问题。这是旧代码。API从那时起发生了变化。从文档:从API 21开始,GridLayout的多余空间分布符合重量原则。嗨,欢迎使用StackOverflow。请不要仅仅将代码作为一个标签发布回答。解释你的想法,以便我们更好地理解你所做的。谢谢。我不同意。他花时间发布代码,如果你愿意,你可以自己弄清楚它是如何工作的。这不是一个很大的片段,也很容易理解understand@boy他只是按照要求在这个网站上发布答案总共12项,那么行数将为5,但我们只需要4项。
gridLayout = (GridLayout) findViewById(R.id.gridview);
gridLayout.removeAllViews();
int total = 10;
int column = 3;
int row = total / column;
gridLayout.setColumnCount(column);
gridLayout.setRowCount(row + 1);
for (int i = 0, c = 0, r = 0; i < total; i++, c++) {
if (c == column) {
c = 0;
r++;
}
ImageView oImageView = new ImageView(this);
oImageView.setImageResource(R.drawable.ic_launcher);
oImageView.setLayoutParams(new LayoutParams(100, 100));
Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
Spec colspan = GridLayout.spec(GridLayout.UNDEFINED, 1);
if (r == 0 && c == 0) {
Log.e("", "spec");
colspan = GridLayout.spec(GridLayout.UNDEFINED, 2);
rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 2);
}
GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
rowSpan, colspan);
gridLayout.addView(oImageView, gridParam);
}