如何在android中创建自定义文本视图?

时间:2010-10-02 17:57:29

标签: android

海友,              我正在解析网址以显示其中的内容,我的要求是我必须在单独的文本视图中显示每个内容。

对于实例:           让我们假设该url中的内容是FootBall,Carom,chess,VolleyBall等。我想像其他人一样将FootBall显示为单独的textview。所以我不能在xml中声明我通常做的文本视图。

(<TextView  android:text=" " android:layout_width="wrap_content"
                 android:gravity="center_horizontal" android:paddingLeft="7dp" android:layout_height="wrap_content"
               />).

所以我打算通过java代码创建textview  这是我的解析代码,它解析url内容并将结果存储在字符串数组中,即san_tagname;根据这个变量的长度,我想创建一些textview。

     List<Message_category> l_obj_tagname = new ArrayList<Message_category>();
        l_obj_tagname = obj_parse1.parse_tagname();
        System.out.println("l_obj_tagname"+l_obj_tagname.size());
        String[] san = new String[l_obj_tagname.size()];
        Iterator<Message_category> it_id1 = l_obj_tagname.iterator();
        i=-1;
        while (it_id1.hasNext()) {

            i++;
            san[i] = it_id1.next().toString();
            System.out.println("Id="+san[i].toString());
            san_tagname[i]=san[i];
            //vm.setTitle(it.next().toString());

        }
         for(int z=0;z<san_tagname.length;z++)
        {
            //how to create textview here ...............
        }

我真的很挣扎,请帮助我关于这个朋友.................

先谢谢 Tilsan The Fighter ......

2 个答案:

答案 0 :(得分:1)

TextView tv = new TextView(context);
tv.setText(myText);
parent.addView(tv, {LayoutParams for parent container type})

答案 1 :(得分:-1)

这是答案, 从网站解析内容

//manipulation to parse id
        List<Message_category> l_obj_id = new ArrayList<Message_category>();
        l_obj_id = obj_parse1.parse_id();
        VAL1 = new String[l_obj_id.size()];
        Iterator<Message_category> it_id = l_obj_id.iterator();
        while (it_id.hasNext()) {
            i++;
            VAL1[i] = it_id.next().toString();
            System.out.println("Id="+VAL1[i].toString());
            //vm.setTitle(it.next().toString());
        }

        //manipulation to parse tagname
        List<Message_category> l_obj_tagname = new ArrayList<Message_category>();
        obj_parse1.parse_tagname();
        obj_parse1.storedata();

    san_tagname= new String[obj_parse1.santagname.length];
    for(int k=0;k<obj_parse1.santagname.length;k++)
    {
        san_tagname[k]=ParsingHandler.temptag[k];

        if(san_tagname[k].contains("%20"))
        {
            san_tagname[k]=san_tagname[k].replace("%20"," ");
            System.out.println("San_tagName1"+san_tagname[k]+"S"+k);


        }
        else
        {
            System.out.println("San_tagName2"+san_tagname[k]+"S"+k);
        }
    }


    gal_lay = (LinearLayout) findViewById(R.id.rl_1);
    navagtion_bar= (LinearLayout) findViewById(R.id.san_tag);
    hv = (HorizontalScrollView)findViewById(R.id.gv);

    // This is the Code i needed finally i stirkes with the help of hackbod
    **for(int z=0;z<san_tagname.length;z++)
    {
        TextView san_text[]= new TextView[san_tagname.length];
        san_text[z]=  (TextView) new TextView(this);
        san_text[z].setText(" "+san_tagname[z]+" ");
        san_text[z].setTextSize(15);
        navagtion_bar.addView(san_text[z]);

    }**