2012-01-02 64 views
1

任何人都可以帮助我如何将动态tablelayout添加到静态main.xml布局中,在静态布局中我添加了按钮和textview。我想将动态tablelayout添加到.xml格式的特定线性布局中。如何将程序化布局集成到静态布局(.xml)中?

+0

您可以发布您的静态布局。 – 2012-01-02 07:50:59

+1

这是我的静态布局 – 2012-01-02 08:07:57

+0

你可以发送你的邮件ID我会发送代码 – 2012-01-02 08:24:59

回答

2

以编程方式,您可以创建一个动态的tablelayout。该表的布局添加到您在XML使用addView()方法定义的布局:

//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); 
+0

+1同意你。 – 2012-01-02 07:53:04

+0

对于'TableLayout'的动态创建,您可以使用:'TextView view = new TextView(context);'创建(例如)新的TextView;并将它们添加到tableLayout中:'tableLayout.add(view)'。如果你需要新的行,你会做同样的事情,但做一个新的['TableRow'](http://developer.android.com/reference/android/widget/TableRow.html) – Jakar 2012-01-02 07:57:49

0

//这是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));