How to create a table in Android with multiple columns?

后端 未结 1 1280
予麋鹿
予麋鹿 2021-02-01 21:27

I want to create a table in android with multiple column. Most of the examples I saw is with 2 columns. (I am new to Java and Android.) I need 3-4 columns and I should be able t

1条回答
  •  迷失自我
    2021-02-01 22:09

    I assume you're talking about a TableLayout view and not a table in a database??

    If so, here's an XML example of a table with three columns and three rows.

    Each < TableRow > element creates a row in the table, and each view inside the element creates a "column". I've used TextViews, but they can be ImageViews, EditText, etc.

    
    
    
    
         
             
             
             
         
    
         
             
             
             
         
    
         
             
             
             
         
    
    

    To dynamically change these in the code, you'd have something like this:

    // reference the table layout
    TableLayout tbl = (TableLayout)findViewById(R.id.RHE);
    // delcare a new row
    TableRow newRow = new TableRow(this);
    // add views to the row
    newRow.addView(new TextView(this)); // you would actually want to set properties on this before adding it
    // add the row to the table layout
    tbl.addView(newRow);
    

    0 讨论(0)
提交回复
热议问题