如何对扩展多行的列使用GridLayout.Spec? [英] How to use GridLayout.Spec for a column extending multiple rows?

查看:145
本文介绍了如何对扩展多行的列使用GridLayout.Spec?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态创建一个包含2行4列的表.但是行和列的大小在每种情况下都不应相同.也许我会附上图片:

I'm trying to dynamically create a table with 2 rows and 4 columns. But the size of rows and columns in each case should not be the same. Perhaps I will attach a picture:

我想要:

据我了解,当我不使用XML标记通过setLayoutParams中的GridLayout.Spec实现此目的时

As I understand, when i don't use XML markup to achieve this using a GridLayout.Spec in setLayoutParams

但是我不明白我需要在GridLayout.Spec中传递哪些参数?

But I don't understand what parameters I need to pass in GridLayout.Spec ?

如果我想将第一行中的第一列与第二行中的第一列合并,应该在其中指定什么内容

If I want to merge the first column in the first row with the first column of the second row, what should I specify in

GridLayout.LayoutParams (GridLayout.Spec rowSpec, GridLayout.Spec columnSpec)

?

什么是Spec?

我看到了

I saw the examples there and there but do not fully understand how it works.

如果使用XML标记,我可以使用layout_rowSpan和layout_columnSpan合并行或列,那么如何动态进行呢?

If in the case of XML markup I can use layout_rowSpan and layout_columnSpan to merge rows or columns then how to do it in dynamically?

推荐答案

请看下面的代码以获取所需的布局.添加注释以解释GridLayout.Spec创建中使用的值.

Take look at this code below to get the layout that you're looking for. Comments are added to explain the values used in the GridLayout.Spec creation.

// simple grid layout with WRAP_CONTENT width and height
GridLayout gridLayout = (GridLayout) findViewById(R.id.grid_layout);

// face view takes 2 rows, 1 column -- zero index based
GridLayout.Spec faceRow = GridLayout.spec(0, 2); // starts row 0, takes 2 rows
GridLayout.Spec faceCol = GridLayout.spec(0); // starts col 0, takes 1 col

GridLayout.Spec titleRow = GridLayout.spec(0); // starts row 0, takes 1 row
GridLayout.Spec titleCol = GridLayout.spec(1, 3); // starts col 1, takes 3 cols

GridLayout.Spec plusRow = GridLayout.spec(1); // starts row 1, takes 1 row
GridLayout.Spec plusCol = GridLayout.spec(1); // starts col 1, takes 1 col

GridLayout.Spec minusRow = GridLayout.spec(1); // starts row 1, takes 1 row
GridLayout.Spec minusCol = GridLayout.spec(2); // starts col 1, takes 1 col

GridLayout.Spec checkRow = GridLayout.spec(1); // starts row 1, takes 1 row
GridLayout.Spec checkCol = GridLayout.spec(3); // starts col 1, takes 1 col

// create the LayoutParams using our row/col for each view
GridLayout.LayoutParams faceParams = new GridLayout.LayoutParams(faceRow, faceCol);
faceParams.setGravity(Gravity.FILL_VERTICAL); // fill vertical so we take up the full 2 rows
// dummy text views to fill some space
TextView faceText = new TextView(this);
faceText.setPadding(32, 32, 32, 32); // add some random padding to make the views bigger
faceText.setLayoutParams(faceParams);
faceText.setText("FACE");
faceText.setGravity(Gravity.CENTER);
faceText.setBackgroundColor(Color.RED);
gridLayout.addView(faceText, faceParams);

GridLayout.LayoutParams titleParams = new GridLayout.LayoutParams(titleRow, titleCol);
titleParams.setGravity(Gravity.FILL_HORIZONTAL); // fill horizontal so we take up the full 3 columns
TextView titleText = new TextView(this);
titleText.setPadding(32, 32, 32, 32);
titleText.setLayoutParams(titleParams);
titleText.setText("TITLE");
titleText.setGravity(Gravity.CENTER);
titleText.setBackgroundColor(Color.BLUE);
gridLayout.addView(titleText, titleParams);

GridLayout.LayoutParams minusParams = new GridLayout.LayoutParams(minusRow, minusCol);
TextView minusText = new TextView(this);
minusText.setPadding(32, 32, 32, 32);
minusText.setLayoutParams(minusParams);
minusText.setText("MIN");
minusText.setGravity(Gravity.CENTER);
minusText.setBackgroundColor(Color.YELLOW);
gridLayout.addView(minusText, minusParams);

GridLayout.LayoutParams plusParams = new GridLayout.LayoutParams(plusRow, plusCol);
TextView plusText = new TextView(this);
plusText.setPadding(32, 32, 32, 32);
plusText.setLayoutParams(plusParams);
plusText.setText("PLS");
plusText.setGravity(Gravity.CENTER);
plusText.setBackgroundColor(Color.GREEN);
gridLayout.addView(plusText, plusParams);

GridLayout.LayoutParams checkParams = new GridLayout.LayoutParams(checkRow, checkCol);
TextView checkText = new TextView(this);
checkText.setPadding(32, 32, 32, 32);
checkText.setLayoutParams(faceParams);
checkText.setText("CHK");
checkText.setGravity(Gravity.CENTER);
checkText.setBackgroundColor(Color.MAGENTA);
gridLayout.addView(checkText, checkParams);

希望这有助于理解GridLayout.Spec.

这篇关于如何对扩展多行的列使用GridLayout.Spec?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆