2012-11-08 47 views
3

我有一个名称,年龄和等级的自定义列表。我想遍历列表并在表格布局中显示内容。每行都有以下属性.Row会有某种造型如何在android表格布局的行中添加多列

Name, 
    Age, 
    Age_Value, 
    Grade, 
    Grade_Value 

的这里年龄和成绩只不过是低于label.Given是一个示例表

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)); 
        } 
        } 

由于这些项目都将在同一行即时得到在单行结果。我该怎么让他们在单独的行

回答

3

你应该尝试这样

代码

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); 
} 
+0

newRow.addView(feeTypeText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f))); ..构造函数ViewGroup.LayoutParams(int,int,float)未定义.am我缺少somthing? – playmaker420

+0

我使用了一些线性布局来实现结果。您的代码确实有帮助。谢谢 – playmaker420

1

您可以添加像这样......在这里,我添加的TableRow通过编程中,我加入的TextView 。

addtree.setOnClickListener(new OnClickListener() 
    { 

     @Override 
     public void onClick(View arg0) 
     { 


       tr1 = new TableRow(YourActivity.this); 
       tr1.setClickable(true); 
       tr1.setId(jj); 
       tr1.setPadding(0, 5, 0, 5); 
       Log.d("id", "id is......"+tr1.getId()); 
       LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 

       TextView textview = new TextView(New_Quote_Activity.this); 

       textview.setText("Job "+jj); 
       textview.setTextColor(Color.BLACK); 
       textview.setTypeface(null, Typeface.BOLD); 
       textview.setTextSize(14); 
       textview.setPadding(5, 0, 0, 0); 


       View vv=new View(New_Quote_Activity.this); 
       vv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,1)); 

       vv.setBackgroundResource(R.color.viewcolor); 

       View vv1=new View(New_Quote_Activity.this); 
       vv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,1)); 

       vv1.setBackgroundResource(R.color.viewcolor); 
       tr1.addView(textview); 

       table_crete_invoice.addView(vv1); 
       table_crete_invoice.addView(tr1,layoutParams); 
       table_crete_invoice.addView(vv); 


      } 
相关问题