我有一个TableLayout,我在其中动态添加行,一开始是空的,当用户单击它时,我想在该行中加载它。
我已经为行分配了OnClick方法,但是我不知道当它进入onclick方法时如何加载它。
public void onClick(View v){
// CODE TO LOAD THE XML
Toast.makeText(getApplicationContext(), "This should appear", Toast.LENGTH_SHORT).show();
}我试过使用inflate,但我似乎没有正确使用它,因为应用程序在单击一行时会崩溃。
如何使行包含xml?
发布于 2011-09-15 20:37:29
在最初添加到TableLayout时,首先设置所有TableRow的标签。因为您已经动态地加载了TableRow,所以我希望它来自于inflate,如下所示
TableRow inflateRow = (TableRow) View.inflate(Activity, R.layout.table_row, null);
//Main TableLayout
TableLayout tblAddLayout = (TableLayout) findViewById(R.id.tblAddLayout);
for (int i=0;i<10;i++){
inflate = (TableRow) View.inflate(myActivity, R.layout.table_row, null);
//set tag for each TableRow
inflate.setTag(i);
//add TableRows to TableLayout
tblAddLayout.addView(inflate);
//set click listener for all TableRows
inflateRow.setOnClickListener(this);
}
public void onClick(View v){
String strTag = v.getTag().toString();
// Find the corresponding TableRow from the Main TableLayout using the tag when you click.
TableRow tempTableRow = (TableRow)tblAddLayout.findViewWithTag(strTag);
//then add your layout in this TableRow tempTableRow.
}在上面的onClick方法中,您可以使用标记加载xml文件。
发布于 2011-05-26 01:57:25
通过ID在TableRow中找到TextView,然后将XML设置为TextView。
https://stackoverflow.com/questions/4293732
复制相似问题