Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

Android adding textview programmatically

I am trying to add textview in a layout programmatically the textview is added on layout but it's not visible.

I have created a method setSelectedContactTextView() which is called from onResume() the method adds textview but not make them visible on screen.

Here is my code:

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++;
                }
            }
        });
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may try refering to this code

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

also make sure that the layout params you're creating are LinearLayout.LayoutParams...

Or try this code

 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);
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...