2014-07-20 37 views
0

我有一个TableLayout动态地添加了一个线性布局,其中包含一个按钮单击的文本视图。添加每个线性布局时。我需要更新最后添加的线性布局的文本视图。我怎样才能做到这一点?在表格布局中动态添加textview

TableLayout tableLayout= (TableLayout) findViewById(R.id.table_layout); 
View subView = (LinearLayout) inflater.inflate(R.layout.layout_view, container, false); 
tableLayout.addView(subView); 
txtVal = (TextView)imageView.findViewById(R.id.txt_val); 

我需要一个按钮被点击

回答

0

你可以得到tableLayout = LinearLayout中的最后一个孩子时,在最后添加子视图设置文本的txtVal。

然后得到LinearLayout = TextView的最后一个孩子。

然后设置TextView的文本。具体方法如下:此代码

ViewGroup mainLayout = (ViewGroup) findViewById(R.id.tableLayout); 
int childrenCount = mainLayout.getChildCount(); 
ViewGroup lastLayout = (ViewGroup) mainLayout.getChildAt(childrenCount-1); 

childrenCount = lastLayout.getChildCount(); 
TextView textView = (TextView) lastLayout.getChildAt(childrenCount-1); 
textView.setText("hello"); 

注意 选择您TableLayout最后孩子你LinearLayout最后孩子。