以编程方式添加视图。什么都不显示

Adding views programmatically. displays nothing

我正在以编程方式向我的 table 行添加一个视图。一切正常,logcat 没有显示任何错误。奇怪的是,我的 activity.

中没有显示任何内容

下面是我使用的代码:

            try{
                int k=0;

                for(i=0;i<=Math.ceil(jArray.length()/2);i++){
                    Log.e("I...",""+i+" "+Math.ceil(jArray.length()/2));
                    tr_head = new TableRow(getApplicationContext());
                    tr_head.setId(10+i);
                    tr_head.setPadding(30, 30, 30, 5);
                    tr_head.setGravity(Gravity.LEFT|Gravity.CENTER_HORIZONTAL);
                    tr_head.setLayoutParams(new LayoutParams(
                                                             LayoutParams.MATCH_PARENT,
                                                             LayoutParams.WRAP_CONTENT));

                    for(j=0;j<2;j++)
                    {
                        if (k<jArray.length()){
                            jobj1 = jArray.getJSONObject(k);

                            LinearLayout parent = new LinearLayout(getApplicationContext());
                            parent.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(150), dpToPx(150)));
                            parent.setOrientation(LinearLayout.VERTICAL);
                            parent.setGravity(Gravity.CENTER);
                            ImageView img = new ImageView(getApplicationContext());
                            img.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(60),dpToPx(60)));
                            Picasso.with(getApplicationContext()).load(jobj1.getString("image")).resize(50,50).into(img);
                            parent.addView(img);

                            TextView txt = new TextView(getApplicationContext());
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                            txt.setLayoutParams(params);
                            txt.setText(jobj1.getString("name"));
                            parent.addView(txt);

                            tr_head.addView(parent);
                            parent.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    try{
                                        String catid=jobj1.getString("category_id");
                                        Intent intent = new Intent(getApplicationContext(), DescribeComplain.class);
                                        intent.putExtra("category_id",catid);
                                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                        startActivity(intent);
                                    }
                                    catch(JSONException je){
                                        je.printStackTrace();
                                    }
                                }
                            });
                        }
                        else{
                            break;
                        }
                        k++;
                    }
                    t1.addView(tr_head, new TableLayout.LayoutParams(
                                                                     LayoutParams.WRAP_CONTENT,
                                                                     LayoutParams.WRAP_CONTENT));
                }
            }
            catch(JSONException je){
                je.printStackTrace();
            }
        }
    });
}
catch(JSONException e){
    Log.e("log_tag", "Error parsing data "+e.toString());
}

您的问题出在这一行:

LinearLayout parent = new LinearLayout(getApplicationContext());

您正在创建一个新视图,但她没有附加到您的根视图。 尝试在您的布局文件中声明您的 LinearLayout,并使用 ID

找到它
LinearLayout parent = (LinearLayout) findViewById(R.id.my_parent_layout);

尝试了很多方法后,我通过设置 linearLayout 的参数解决了这个问题

LinearLayout parent = new LinearLayout(SelectCaegory.this);
TableRow.LayoutParams Paramslinear = new  TableRow.LayoutParams(dpToPx(150),dpToPx(150));