Android以编程方式添加textview

时间:2013-05-24 08:25:15

标签: android android-layout textview

我尝试以编程方式在布局中添加textview,但在布局上添加了textview,但它不可见。

我创建了一个方法setSelectedContactTextView(),该方法从onResume()调用,该方法会添加textview但不会在屏幕上显示它们。

这是我的代码:

protected void onResume() {

        if(Constants.fbContactListArrayList!=null && Constants.fbContactListArrayList.size()!=0){
            setSelectedContactTextView(Constants.fbContactListArrayList);
        }
        super.onResume();
    }

    //SET SELECTED CONTACTS IN TEXTVIEW FORMS
    public void setSelectedContactTextView(final ArrayList<Object> list){
        //Constants.progressDialog=ProgressDialog.show(this, "", Constants.MSG_PROGESSDIALOG);
        new Thread(new Runnable() {

            @Override
            public void run() {
                while(i<list.size()){
                    ContactBean contactBean=(ContactBean)list.get(i);
                    if(contactBean.isSelected()==true){
                        TextView contactTextView=new TextView(NewEventShowDetails.this);
                        contactTextView.setText(contactBean.getUserName().toString());
                        fbContactTextLinearLayout.addView(contactTextView);
                    }
                    i++;
                }
            }
        });
    }

1 个答案:

答案 0 :(得分:2)

您可以尝试参考此代码

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.info)
...
linearLayout.addView(valueTV);

还要确保您创建的layout参数是LinearLayout.LayoutParams...

或尝试此代码

 LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
 String[] informations = new String[] { "one", "two", "three" };
 TextView informationView;

  for (int i = 0; i < informations.length; i++) {
    View line = new View(this);
    line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
    line.setBackgroundColor(0xAA345556);
    informationView = new TextView(this);
    informationView.setText(informations[i]);
    layout.addView(informationView, 0);
    layout.addView(line, 1);
 }