Android以编程方式添加textview [英] Android adding textview programmatically

查看:132
本文介绍了Android以编程方式添加textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式在布局中添加textview,但textview已添加到布局中,但它不可见.

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

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

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

这是我的代码:

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

推荐答案

您可以尝试参考此代码

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

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

also make sure that the layout params you're creating are 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);
 }

这篇关于Android以编程方式添加textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆