LayoutParams没有显示

时间:2011-10-10 04:26:57

标签: android layoutparams

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayout);



   for(int k=0;k<10;k++)    
   {    TableRow tr=new TableRow(this);
       tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
        tr.setId(k);

    for(int s=0;s<10;s++)
         {
         EditText edt=new EditText(this);
         Log.i("SS","setting layout params for textview "+s+k);
         edt.setText("abc "+s+k+"  ");


        edt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        ViewGroup.LayoutParams p=edt.getLayoutParams();

         Log.i("SS","edt params "+p.height +" "+p.width);
        tr.addView(edt);
         }
   ll.addView(tr);
   }



}

}

在上面的代码中,如果我删除行edt.setLayoutParams ...编辑文本正在显示。 但是,如果我设置他们的参数,他们没有显示。 任何想法可能是什么原因?

1 个答案:

答案 0 :(得分:0)

试试这个......

TableRow tr = new TableRow(this);  
TableLayout.LayoutParams tableRowParams=
  new TableLayout.LayoutParams
  (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);


tableRowParams.setMargins(10, 10, 10, 10);

tr.setLayoutParams(tableRowParams);

希望这会帮助你...