【发布时间】:2013-07-16 19:12:10
【问题描述】:
任何人都可以帮助我如何将动态表格布局添加到静态 main.xml 布局中,在静态布局中我添加了按钮和 textview。我想将动态表格布局添加到 .xml 表单中的特定线性布局中。
【问题讨论】:
-
你能发布你的静态布局吗?
-
这是我的静态布局
-
你能把你的邮件ID发给我吗
任何人都可以帮助我如何将动态表格布局添加到静态 main.xml 布局中,在静态布局中我添加了按钮和 textview。我想将动态表格布局添加到 .xml 表单中的特定线性布局中。
【问题讨论】:
您可以通过编程方式创建动态表格布局。使用 addView() 方法将该表格布局添加到您在 XML 中定义的布局中:
示例
//get the linearlayout from xml
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.my_layout);
//dynamically create your table layout
TableLayout tablelayout = new TableLayout(this);
//add tablelayout to linearlayout
linearLayout.addView(tablelayout);
【讨论】:
TableLayout 的动态创建,您将使用:TextView view = new TextView(context); 创建(例如)新的 TextView,并将它们添加到 tableLayout,如:tableLayout.add(view)。如果你需要新的行,你会做同样的事情,但创建一个新的TableRow
//这是你在xml中的布局
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.list_parent);
//这是您在活动中动态创建的
TableLayout tablelayout = new TableLayout(this);
//接下来将新的表格布局添加到您的线性布局中
linearLayout.addView(tablelayout, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
【讨论】: