gpt4 book ai didi

android - 如何在android表格布局的一行中添加多列

转载 作者:太空宇宙 更新时间:2023-11-03 13:31:02 26 4
gpt4 key购买 nike

我有一个包含姓名、年龄和年级的自定义列表。我想遍历列表并在表格布局中显示内容。每一行将具有以下属性。行将具有某种样式

  Name,
Age,
Age_Value,
Grade,
Grade_Value

这里Age和Grades只是一个标签,下面是一个示例表

Dolly
Age
23
Grade
A+

Roman
Age
22
Grade
C

Ben
Age
23
Grade
B+

我想将这些详细信息动态添加到我的布局中的表格布局中。

这是我正在尝试的代码

 for(int j=0;j<_attributeList.size();j++)
{

TableRow tr = new TableRow(MyActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

TextView tvName = new TextView(MyActivity.this);
tvName.setPadding(3,0,0,0);
tvName.setText(_attributeList.get(j).getName() +" : ");
tr.addView(tvName);

TextView tvAgeL = new TextView(MyActivity.this);

tvAgeL .setText("Age");
tr.addView(tvAgeL );

TextView tvAgeValue = new TextView(MyActivity.this);
tvAgeValue .setPadding(3,0,0,0);
tvAgeValue .setText(_attributeList.get(j).getValue() +" : ");
tr.addView(tvAgeValue );



attributeTable.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}

由于这些项目被添加到同一行,我在一行中得到结果。我怎样才能在单独的行中显示它们

最佳答案

你应该这样尝试

代码

for(int i = 0; i < _attributeList.size();i++){              

TableRow newRow = new TableRow(getApplicationContext());
newRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

newCheckBox = new CheckBox(getApplicationContext());

TextView feeTypeText = new TextView(getApplicationContext());
feeTypeText.setText(jsonObj.getString("feeType"));
feeTypeText.setTextColor(Color.BLACK);
feeTypeText.setTextSize(16f);
feeTypeText.setTypeface(tf);

TextView dueAmountText = new TextView(getApplicationContext());
dueAmountText.setText(jsonObj.getString("dueAmount"));
dueAmountText.setTextColor(Color.BLACK);
dueAmountText.setTextSize(16f);
dueAmountText.setTypeface(tf);

TextView dueDateText = new TextView(getApplicationContext());
dueDateText.setText(jsonObj.getString("dueDate"));
dueDateText.setTextColor(Color.BLACK);
dueDateText.setTextSize(16f);
dueDateText.setTypeface(tf);

newRow.addView(newCheckBox,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
newRow.addView(feeTypeText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
newRow.addView(dueAmountText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f)));
newRow.addView(dueDateText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f)));
attributeTable.addView(newRow);
}

关于android - 如何在android表格布局的一行中添加多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13288899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com