1

I have table layout. We should be able to add rows that containing 3 textviews and 4 imageviews to that tablelayout dynamically when onclick of the "+" button. We can able to remove a particular selected row when onclick of "-" button. When we select a particular row, we should be able to add images onclick of camera button to that particular row. How to do it. Can anyone please help me..

4

1 回答 1

0

您可以使用 LayoutInflater 在单击 + 按钮时添加行。或者您可以使用代码添加视图。这是一个将 ImageView 动态添加到表格布局的示例代码。

             private void addImageView()
{
    ImageView mImageView = new ImageView(this);
    TableLayout mTableLayout = (TableLayout) findViewById(R.id.tablelayout1);
    mImageView.setId(getRndId());
    TableRow mTableRow = new TableRow(this);
    mTableRow.setId(getRndId());
    mTableRow.addView(mImageView);
    mTableLayout.addView(mTableRow);

}

/**
 * Gets a random no and checks if its already used in R.java
 * */
protected int getRndId()
{
    Random rnd = new Random();
    int possible_id = rnd.nextInt();
    while(true)
    {
    //  Log.d(TAG, "possible_id=" + possible_id);
        View temp = findViewById(possible_id);
        if ((possible_id>0) && (temp==null))
        {
            return possible_id;
        }
        else
        {
            possible_id = rnd.nextInt();
        }       
    }
}
于 2012-07-03T11:55:43.140 回答