Android的充气后动态TextView的名字 [英] dynamically name textview after inflating android

查看:101
本文介绍了Android的充气后动态TextView的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我米充气4线性布局中多次滚动视图。每个布局在它的头标题。在code片段如下。

I m inflating 4 linear layouts multiple times in a scroll view. Each layout has a title in its header. The code snippet is given below.

for(int j=0;j<qType.length;j++)
    {           
        LinearLayout.LayoutParams siz = new LinearLayout.LayoutParams(width, height);;
        if(qType[j]==0)
        {               
            view1 = getLayoutInflater().inflate(R.layout.layout1, main_layout,false);
            siz = new LinearLayout.LayoutParams(width, height/3);                                       
        }
        else if(qType[j]==1)
        {                
            view1 = getLayoutInflater().inflate(R.layout.layout3, main_layout,false);
            siz = new LinearLayout.LayoutParams(width, height/3);
        }
        else if(qType[j]==2)
        {           
            view1 = getLayoutInflater().inflate(R.layout.layout4, main_layout,false);
            siz = new LinearLayout.LayoutParams(width, height/3);
        }
        else if(qType[j]==3)
        {           
            view1 = getLayoutInflater().inflate(R.layout.layout5, main_layout,false);
            siz = new LinearLayout.LayoutParams(width, height/2);
        }       

        siz.topMargin = 25;
        main_layout.addView(view1, siz);            
    }
    scroll_layout.addView(main_layout);
    scroll_layout.setBackgroundResource(R.drawable.options_background);     
    setContentView(scroll_layout);  

现在没有在每个布局TextView的,我想改变它的文本。如果我通过findviewbyid访问他们,给的setText,只有第一个实例正在改变,我想改变在所有场合TextView的。

Now there is textview in each layout and i want to change the text of it. If i access them by findviewbyid and give settext, only the first instance is being changed, i want to change the textview on all occasions.

    for(int k=0;k<NumberQuestions.length;k++)
    {
        TextView number_title = (TextView)main_layout.findViewById(R.id.number_title);
        TextView mcq_title = (TextView)main_layout.findViewById(R.id.mcq_title);
        TextView comment_title = (TextView)main_layout.findViewById(R.id.comment_title);
        TextView cam_title = (TextView)main_layout.findViewById(R.id.cam_title);
        if(qType[k]==0)
        {               
            number_title.setTypeface(myriadpro_bold);
            number_title.setText(NumberQuestions[k]);

        }
        if(qType[k]==1)
        {
            comment_title.setTypeface(myriadpro_bold);
            comment_title.setText(NumberQuestions[k]);              
        }
        else if(qType[k]==2)
        {
            mcq_title.setTypeface(myriadpro_bold);
            mcq_title.setText(NumberQuestions[k]);              
        }
        else if(qType[k]==3)
        {
            cam_title.setTypeface(myriadpro_bold);
            cam_title.setText(NumberQuestions[k]);              
        }           

请帮忙。

推荐答案

如果我是正确main_layout是在XML命名视图file.So你应该这样使用

If i am right main_layout is a name view in your xml file.So You should use like this

 view1 = getLayoutInflater().inflate(R.layout.layout1, main_layout,false);
 TextView tv = (TextView)main_layout.findViewById(R.id.tv1);

,其中电视是texview在layout1.xml

where tv is texview in layout1.xml

这篇关于Android的充气后动态TextView的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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