2012-01-02 112 views
0

我该怎么做才能将此布局添加到main.xml中。这个类文件是从主要活动类别开始的。我是否需要将此类调用到主活动类中以添加此动态表格布局。并告诉我如何将该类添加到主活动课如何将自定义tablelayout添加到.xml布局中?

import android.R.color; 
import android.app.Activity; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.text.TextWatcher; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.GridView; 
import android.widget.LinearLayout; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 
import android.widget.SimpleAdapter.ViewBinder; 
import android.widget.TableRow.LayoutParams; 

public class TestGridActivity extends Activity implements View.OnClickListener{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.main); 

     TableLayout layout = new TableLayout (this); 
     layout.setLayoutParams(new TableLayout.LayoutParams(85,85)); 

     layout.setPadding(8,8,8,8); 

     for (int f=0; f<=6; f++) { 

      TableRow tr = new TableRow(this); 

      tr.setBackgroundColor(Color.BLACK); 
      tr.setPadding(0,0, 0,2); 

      TableRow.LayoutParams llp = new  TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
      llp.setMargins(0, 0, 2, 0);//2px right-margin 

      //New Cell 

      for (int c=0; c<=288; c++) { 

       LinearLayout cell = new LinearLayout(this); 
       cell.setBackgroundColor(Color.WHITE); 
       cell.setLayoutParams(llp);//2px border on the right for the cell 

       TextView b = new TextView (this); 
       b.setText("Sample"); 
       b.setTextSize(10.0f); 
        b.setHeight(60); 
        b.setWidth(70); 
       b.setPadding(0, 0, 4, 0); 

       cell.addView(b); 
       tr.addView(cell); 

      } // for 
      layout.addView(tr); 
     } // for 

     super.setContentView(layout); 
    } //() 

    public void onClick(View view) { 
     ((TextView) view).setText("*"); 
     ((TextView) view).setEnabled(false); 
    } 
} // class 

我应该怎么做这个tablelayout传递到main.xml中的布局

回答

0

集main.xml中一个内容查看并添加动态tablelayout为在main中声明的布局的视图 。 XML。

使你的的onCreate()代码就会像下面

public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
LinearLayout ll = (LinearLayout) findViewById(R.id.add_layout); 

      TableLayout layout = new TableLayout (this); 
      layout.setLayoutParams(new TableLayout.LayoutParams(85,85)); 

      layout.setPadding(8,8,8,8); 

      for (int f=0; f<=6; f++) { 

       TableRow tr = new TableRow(this); 

       tr.setBackgroundColor(Color.BLACK); 
       tr.setPadding(0,0, 0,2); 

       TableRow.LayoutParams llp = new   
         TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
       llp.setMargins(0, 0, 2, 0);//2px right-margin 

       //New Cell 

       for (int c=0; c<=288; c++) { 

        LinearLayout cell = new LinearLayout(this); 
        cell.setBackgroundColor(Color.WHITE); 
        cell.setLayoutParams(llp);//2px border on the right for the cell 

        TextView b = new TextView (this); 
        b.setText("Sample"); 
        b.setTextSize(10.0f); 
         b.setHeight(60); 
         b.setWidth(70); 
        b.setPadding(0, 0, 4, 0); 

        cell.addView(b); 
        tr.addView(cell); 

       } // for 
       layout.addView(tr); 
      } // for 
      ll.addView(layout); 
//   super.setContentView(layout); 
     } //() 
+0

太谢谢你了:-)其工作,非常感谢 – 2012-01-02 09:12:03

相关问题