今天安卓项目改一功能,往表格中动态添加行,其中有设置该行布局参数的代码,但是发现getLayoutParams方法返回的是null.
网上查到了原因:https://blog.csdn.net/qq_20089667/article/details/70051689
调用该方法前,该元素必须得有一共父元素。
解决的办法是:先调用表格的addView方法,然后再设置布局参数:
TableRow row = new TableRow(this);
//必须先加进父元素,后面的layoutparam才能设置
tableLayout.addView(row);
(LinearLayout.LayoutParams)row.getLayoutParams();
//...