膨胀TableRow

shi*_*kan 1 android

我有一个TableLayout,我在其中动态添加行,首先为空,我想在用户点击它时在该行中加载一个xml.

我已将OnClick方法分配给该行,但我不知道如何在进入onclick方法时加载xml.

public void onClick(View v){
 // CODE TO LOAD THE XML

 Toast.makeText(getApplicationContext(), "This should appear", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)

我尝试过使用inflate,但似乎我没有正确使用它,因为点击一行时应用程序崩溃了.

我怎么能让行包含xml?

Dur*_*rai 9

首先在最初添加到TableLayout时为所有TableRow设置标记.因为你已经动态加载了TableRow,我希望它来自膨胀,如下所示

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.
}
Run Code Online (Sandbox Code Playgroud)

在上面的onClick方法中,您可以使用标记加载xml文件.