Android - 动态ADDVIEW问题

时间:2010-09-10 14:42:38

标签: android

我有一些问题,我无法解决这个问题。 在我的代码中,我添加动态ImageButtons(我动态创建TableRow并添加行),它们显示但我无法点击它们(我尝试所有,将clickable设置为true,启用为true,没有任何内容)。如果我使用Button而不是ImageButton一切正常,除非我再次为按钮设置背景相同的问题。

我也从代码中添加TextView,但TextView根本没有显示。 有什么解决方案吗?

1 个答案:

答案 0 :(得分:0)

这是从某个列表

创建行的功能
public static TableRow[] Create(List<Apartment> list){
    TableRow[] rows=null;
    try{
        rows=new TableRow[list.size()*3];
        int i=0;

        for(Apartment ap : list){
            rows[3*i]=new TableRow(activity);
            rows[3*i+1]=new TableRow(activity);
            rows[3*i+2]=new TableRow(activity);

            rows[3*i].setLayoutParams(new LayoutParams( 
                    LayoutParams.WRAP_CONTENT, 
                    LayoutParams.WRAP_CONTENT));
            rows[3*i+1].setLayoutParams(new LayoutParams( 
                    LayoutParams.WRAP_CONTENT, 
                    LayoutParams.WRAP_CONTENT));
            rows[3*i+2].setLayoutParams(new LayoutParams( 
                    LayoutParams.WRAP_CONTENT, 
                    LayoutParams.WRAP_CONTENT));

            EditText txtMainInform=new EditText(activity);

            //txtMainInform.setText(ap.GetMainInformation());
            txtMainInform.setLayoutParams(new LayoutParams( 
                    LayoutParams.WRAP_CONTENT, 
                    LayoutParams.WRAP_CONTENT));
            txtMainInform.setText("vvvvvvvvvvvvvvvvvvvv");
            txtMainInform.setVisibility(1);
            rows[3*i].addView(txtMainInform);
            rows[3*i].setVisibility(1);

            TextView txtMoreInform=new TextView(activity);
            txtMoreInform.setLayoutParams(new LayoutParams( 
                    LayoutParams.WRAP_CONTENT, 
                    LayoutParams.WRAP_CONTENT));
            rows[3*i+1].addView(txtMoreInform);


            ImageButton imbGallery=new ImageButton(activity);
            imbGallery.setClickable(true);
            imbGallery.setEnabled(true);
            imbGallery.setBackgroundResource(R.drawable.gallery_icon);



            ImageButton imbCall=new ImageButton(activity);
            imbCall.setBackgroundResource(R.drawable.phone);

            ImageButton imbMap=new ImageButton(activity);
            imbMap.setBackgroundResource(R.drawable.map);
            rows[3*i+2].addView(imbGallery);
            rows[3*i+2].addView(imbCall);
            rows[3*i+2].addView(imbMap);
            rows[3*i+2].setEnabled(true);
            i++;
        }

    }
    catch(Exception e){

    }
    return rows;
}

我添加了ImageButtons-这是OnCreate函数的一部分

TableRow[] rows=ResultCreator.Create(list);
TableLayout table=(TableLayout)this.findViewById(R.id.result_layout);

for(TableRow row:rows){
table.addView(row,new TableLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT));
}

我的TableLayout在其他LinearLayout中,有区别吗?
谢谢