2010-07-07 53 views
2

我嘗試從Java代碼添加表格佈局。當一個表格中有兩個字段時,它只顯示第一個字段。如何顯示所有字段的整個行?Android中的動態表格佈局?

..... 
    .... 
    for(int idx=0;idx<4;idx++){ 
     TableRow tbrow = new TableRow(this); 

     TextView text_v1 = new TextView(this); 
     text_v1.setText("TextView"); 

     TextView text_v2 = new TextView(this); 
     text_v2.setText(" Idx : "+idx); 

     tbrow.addView(text_v1); 
     tbrow.addView(text_v2); 

     linearlayout.addView(tbrow); 
    } 
    ......... 
     ........ 

如何做到這一點?

回答

5
LinearLayout ll = new LinearLayout(YourActivity.this); 
     ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); 
     ll.setBackgroundColor(Color.parseColor("#000000")); 
     ll.setOrientation(LinearLayout.VERTICAL); 

    ScrollView sv = new ScrollView(YourActivity.this); 
    sv.setSmoothScrollingEnabled(true); 

    TableLayout tl = new TableLayout(YourActivity.this); 
    tl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 
    tl.setStretchAllColumns(true); 

    TextView header= new TextView(YourActivity.this); 
    header.setText("This is heading "); 
    header.setTextColor(Color.WHITE); 
    header.setBackgroundColor(Color.parseColor("#323232")); 
    header.setTextSize(17.0f); 
    header.setGravity(Gravity.CENTER); 
    header.setClickable(true); 

    for(int sub = 0; sub <=6; sub++) { // Six Rows will be genrated. 
    TableRow tr = new TableRow(YourActivity.this); 
    tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 
    if(sub != 0){ 
     TextView col1 = new TextView(YourActivity.this); 
     col1.setText("Column1"); 
     col1.setTextSize(17.0f); 
     col1.setPadding(5, 0, 0, 2); 
     col1.setTextColor(Color.GRAY); 
     col1.setGravity(Gravity.CENTER_VERTICAL); 
     col1.setLayoutParams(new LayoutParams(50,50)); 
     tr.addView(col1); 

     TextView col2 = new TextView(YourActivity.this); 
     col2.setText("Column2"); 
     col2.setTextSize(17.0f); 
     col2.setPadding(0, 2, 0, 2); 
     col2.setTextColor(Color.GRAY); 
     col2.setGravity(Gravity.CENTER_VERTICAL); 
     col2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,50)); 
     tr.addView(col2); 

     View v = new View(YourActivity.this); 
     v.setBackgroundColor(Color.GRAY); 
     v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,1)); 
     tl.addView(v); 
    } 
     tl.addView(tr);// add rows to the table. 
    } 
     TableRow row1 = new TableRow(Activity.this); // seventh row with //only one column 
     ImageButton startButton = new ImageButton(YourActivity.this); 
     startButton.setBackgroundColor(Color.TRANSPARENT); 
     startButton.setImageResource(R.drawable.icon); 
     startButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
     row1.addView(startButton); 




row1.setGravity(Gravity.CENTER); 
     tl.addView(row1); 

     sv.addView(tl); //add table to scrollview to make it scrollable. 
      ll.addView(header); // add heading to the LinearLayout 
      ll.addView(sv);  // add scrollview to LinearLayout 
      setContentView(ll); enter code here 
2

這可能對您有用..

try 
{ 

    JSONArray jArray = new JSONArray(result); 

    TableLayout tv=(TableLayout) findViewById(R.id.table); 
    tv.removeAllViewsInLayout(); 
    int flag=1; 

    for(int i=-1;i<jArray.length();i++) 
     // when i=-1, loop will display heading of each column 
     // then usually data will be display from i=0 to jArray.length() 
    { 

     TableRow tr=new TableRow(Yourclassname.this); 

     tr.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 


     if(flag==1) 
      // this will be executed once 
      { 

      TextView b3=new TextView(Yourclassname.this); 
      b3.setText("column heading 1"); 
      b3.setTextColor(Color.BLUE); 
      b3.setTextSize(15); 
      tr.addView(b3); 


      TextView b4=new TextView(Yourclassname.this); 
      b4.setPadding(10, 0, 0, 0); 
      b4.setTextSize(15); 
      b4.setText("column heading 2"); 
      b4.setTextColor(Color.BLUE); 
      tr.addView(b4); 

      TextView b5=new TextView(Yourclassname.this); 
      b5.setPadding(10, 0, 0, 0); 
      b5.setText("column heading 3"); 
      b5.setTextColor(Color.BLUE); 
      b5.setTextSize(15); 
      tr.addView(b5); 


      tv.addView(tr); 

      final View vline = new View(Yourclassname.this); 
      vline.setLayoutParams(new  
        TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2)); 
      vline.setBackgroundColor(Color.BLUE); 
      tv.addView(vline); // add line below heading 
      flag=0; 


      } 

     else 
     { 



      JSONObject json_data = jArray.getJSONObject(i); 

      TextView b=new TextView(Yourclassname.this); 
      String str=String.valueOf(json_data.getInt("column1")); 
      b.setText(str); 
      b.setTextColor(Color.RED); 
      b.setTextSize(15); 
      tr.addView(b); 


      TextView b1=new TextView(Yourclassname.this); 
      b1.setPadding(10, 0, 0, 0); 
      b1.setTextSize(15); 
      String str1=json_data.getString("column2"); 
      b1.setText(str1); 
      b1.setTextColor(Color.WHITE); 
      tr.addView(b1); 

      TextView b2=new TextView(Yourclassname.this); 
      b2.setPadding(10, 0, 0, 0); 
      String str2=String.valueOf(json_data.getInt("column3")); 
      b2.setText(str2); 
      b2.setTextColor(Color.RED); 
      b2.setTextSize(15); 
      tr.addView(b2); 

      tv.addView(tr); 


      final View vline1 = new View(Yourclassname.this); 
      vline1.setLayoutParams(new     
        TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1)); 
      vline1.setBackgroundColor(Color.WHITE); 
      tv.addView(vline1); // add line below each row 


     } 

    } 



} 
catch(JSONException e) 
{ 
    Log.e("log_tag", "Error parsing data "+e.toString()); 
    Toast.makeText(getApplicationContext(), "JsonArray fail", 
      Toast.LENGTH_SHORT).show(); 
} 
+0

謝謝這爲我工作。 – 2014-05-27 06:07:48