【问题标题】:Adding rows and cells into TableLayout将行和单元格添加到 TableLayout
【发布时间】:2013-11-28 00:10:18
【问题描述】:

我该怎么做:

  1. 以编程方式在 TableLayout 中创建一行
  2. 在行中创建三个单元格
  3. 在每个单元格中添加任意文本

感谢您的帮助!

【问题讨论】:

    标签: android android-tablelayout


    【解决方案1】:

    试试这个..

    TableLayout tableLayout = new TableLayout(this);
              tableLayout.setStretchAllColumns(true);
              tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
              tableLayout.setWeightSum(3);
    
              for (int i = 0; i < 3; i++) {
                  TableRow tableRow = new TableRow(this);
                  tableRow.setGravity(Gravity.CENTER);
                  tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, 1.0f));
    
                  for (int j = 0; j < 3; j++) {
                      TextView button = new TextView(this);
                      final int buttonNumber = (j + i * 4);
                      button.setText("" + buttonNumber);
                      button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
    
                      tableRow.addView(button);
                  }
                  tableLayout.addView(tableRow);
              }
    
              main_lay.addView(tableLayout);
    

    【讨论】:

      【解决方案2】:
      TableLayout table = (TableLayout) findViewById(R.id.TableName);
      
      TableRow tableRow = new TableRow(context);
      tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
      
      TextView cell1= new TextView(getApplicationContext());
                      cell1.setText("one");
                      cell1.setTextColor(Color.BLACK);
                      cell1.setTextSize(16f);
                      cell1.setTypeface(tf);
      
      TextView cell2= new TextView(getApplicationContext());
                      cell2.setText("two");
                      cell2.setTextColor(Color.BLACK);
                      cell2.setTextSize(16f);
                      cell2.setTypeface(tf);
      
      TextView cell3= new TextView(getApplicationContext());
                      cell3.setText("three");
                      cell3.setTextColor(Color.BLACK);
                      cell3.setTextSize(16f);
                      cell3.setTypeface(tf);
      
       tableRow.addView(cell1);
       tableRow.addView(cell2);
       tableRow.addView(cell3);
      
       talbe.addView(tableRow , new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
      

      【讨论】:

        【解决方案3】:

        如果你愿意,你可以使用这样的函数来更新你的 tableLayout,它会添加一个带有一些文本的新行。

         private void addRowToTableLayout(TableLayout tableLayout, String text){
        
         TableRow tr = new TableRow(MyActivity.this);
         //if you want you can customize the table row
         tr.setBackgroundColor(getResources().getColor(R.color.backgroundColor));
         tr.setLayoutParams(new TableRow.LayoutParams(
                        TableRow.LayoutParams.MATCH_PARENT,
                        TableRow.LayoutParams.WRAP_CONTENT));
        
         TextView textView = new TextView(MyActivity.this);
         //if you want you can customize the textview also
         textView.setText(text);
         tr.addView(textView);
         tableLayout.addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT,
                                            LayoutParams.WRAP_CONTENT));
        }
        

        【讨论】:

          猜你喜欢
          • 2019-09-13
          • 1970-01-01
          • 2012-02-25
          • 2014-03-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-16
          相关资源
          最近更新 更多