Programmatically set margin for TableRow

前端 未结 2 609
走了就别回头了
走了就别回头了 2020-12-08 16:25

I have TableRows created dynamically in the code and I want to set margins for these TableRows.

My TableRows created as follow

2条回答
  •  佛祖请我去吃肉
    2020-12-08 16:49

    This is working:

    TableRow tr = new TableRow(...);
    TableLayout.LayoutParams lp = 
    new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                                 TableLayout.LayoutParams.WRAP_CONTENT);
    
    lp.setMargins(10,10,10,10);             
    tr.setLayoutParams(lp);
    
    ------
    
    // the key is here!
    yourTableLayoutInstance.addView(tr, lp);
    

    You need to add your TableRow to TableLayout passing the layout parameters again!

提交回复
热议问题