【发布时间】:2017-03-07 18:03:08
【问题描述】:
虽然这看起来很多重复的问题,但对我来说是第一次。找遍了,没找到结果,就发到这里了。
我正在动态创建一个表,其中TableLayout 部分用xml 部分编写。
<RelativeLayout
android:layout_width="70dp"
android:layout_height="40dp"
android:background="@color/colorPrimaryDark"
android:id="@+id/componentA">
<TableLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:id="@+id/tl_componentA">
</TableLayout>
</RelativeLayout>
我为表格创建了一个对象
TableLayout tableLayoutA;
tableLayoutA= (TableLayout)view.findViewById(R.id.tl_componentA);
现在我尝试从这里开始动态添加一行
createTableRowColumn(tableLayoutA);
相关的功能是
private void createTableRowColumn(TableLayout tableLayout){
TableRow tableRow1= new TableRow(this.context);
tableRow1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
setTableRowColumnProperty(tableRow1);
tableLayout.addView(tableRow1);
}
private void setTableRowColumnProperty(TableRow tableRow){
TableRow.LayoutParams layoutParams= new TableRow.LayoutParams(70, 40);
tableRow.setLayoutParams(layoutParams);
}
我做了所有这些,但模拟器中没有任何显示。但是当我手动在 xml 中给出相同的结构时,事情就很好了。
出于这个原因,我试图弄清楚
Toast.makeText(getContext(), tableLayoutA.getHeight()+"", Toast.LENGTH_LONG).show();
在 toast 中它显示为 0。我不明白为什么,尽管我已在 xml 本身中修复了 tableLayoutA 的大小。
【问题讨论】:
-
您能详细说明一下吗?
-
无论如何我理解动态,当数据在您的 TableLayout 中不断变化时,这就是您想说的吗?或
-
能否请您更正代码并将其发布,以便我可以找到我做错的地方?
-
是的,可以随时删除添加的行。
-
Mike.. 那根本没有效果
标签: android tablelayout android-tablelayout