0

我在滚动视图中多次膨胀 4 个线性布局。每个布局的标题中都有一个标题。代码片段如下所示。

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

现在每个布局中都有文本视图,我想更改它的文本。如果我通过 findviewbyid 访问它们并提供 settext,则只有第一个实例正在更改,我想在所有情况下更改 textview。

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

请帮忙。

4

3 回答 3

0

免责声明:我觉得您是在尝试使用 Android 框架,而不是使用它。我的建议有点大修,但我确实觉得这是做事的正确方法。

我建议使用 ListView,因为您多次重复相同的格式。列表视图通常具有对象对象的容器,以及负责将对象映射到列表视图中的特定行的适配器。当您更改底层容器中的数据时,您将调用notifyDataSetChanged()适配器的回调以通知它您更改了底层数据并提醒它更新您的列表视图。

来自 android 开发者网站的 2 个很棒的资源/教程: ListView: http: //developer.android.com/guide/topics/ui/layout/listview.html 适配器:http: //developer.android.com/guide/topics/ ui/declaring-layout.html#AdapterViews

于 2013-03-20T17:43:47.403 回答
0

可能所有布局中的 TextViewandroid: id都是相同的,因此解释了 findViewById 方法的行为总是返回第一次出现

于 2013-03-20T17:44:39.433 回答
0

如果我是对的 main_layout 是您的 xml 文件中的名称视图。所以您应该像这样使用

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

其中 tv 是 layout1.xml 中的 texview

于 2013-03-20T18:13:21.030 回答