Android 执行布局充气器时文本视图中的文本未更改

Android 执行布局充气器时文本视图中的文本未更改,android,layout-inflater,Android,Layout Inflater,在我的应用程序中,当活动加载时,我将执行以下操作 setContentView(R.layout.main2); layoutToAdd = (LinearLayout) findViewById(R.id.linearLayout1); for(i=0; i<num;i++) { LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); View view = inflater.inf

在我的应用程序中,当活动加载时,我将执行以下操作

setContentView(R.layout.main2);
layoutToAdd = (LinearLayout) findViewById(R.id.linearLayout1);

for(i=0; i<num;i++)
{
    LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
    View view = inflater.inflate(R.layout.camera, null);
    layoutToAdd.addView(view);
}
setContentView(R.layout.main2);
LayoutLoadd=(LinearLayout)findViewById(R.id.linearLayout1);

对于(i=0;i只需在充气布局后进行设置

View view = inflater.inflate(R.layout.camera, null);
TextView tv = view.findViewById(R.id.textviewid); //id defined in camera.xml
Button b = view.findViewById(R.id.buttonid);      //id defined in camera.xml
tv.setText();
b.setText();
layoutToAdd.addView(view);

只需在充气后设置它们

View view = inflater.inflate(R.layout.camera, null);
TextView tv = view.findViewById(R.id.textviewid); //id defined in camera.xml
Button b = view.findViewById(R.id.buttonid);      //id defined in camera.xml
tv.setText();
b.setText();
layoutToAdd.addView(view);

我的建议是,您应该创建一个只包含线性布局的xml文件

然后以编程方式创建文本视图、编辑文本和按钮,并将其添加到LinearLayout

您可以设置这些组件的不同属性,如下所示:

下面是设置TextView属性的示例。对于其余组件,您可以设置相同的属性

TextView tv=new TextView(context);
tv.setBackgroundResource(R.drawable.textview_bg);
tv.setPadding(20, 5, 40, 5);
tv.setText("set your text");
tv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setTextColor(Color.RED);
tv.setClickable(true);
tv.setId(id);//where id is an integer which should be unique for each TextView

layout.addView(tv); 

此外,您还需要在for循环中创建并添加所有这三个组件,根据迭代循环时使用的i提供唯一的ID!并且您可以为文本视图和按钮设置字符串数组,以便在for循环中设置它们的名称,这将更容易在循环中传递要设置的字符串。

我的建议是应该创建一个只包含LinearLayout的xml文件

然后以编程方式创建文本视图、编辑文本和按钮,并将其添加到LinearLayout

您可以设置这些组件的不同属性,如下所示:

下面是设置TextView属性的示例。对于其余组件,您可以设置相同的属性

TextView tv=new TextView(context);
tv.setBackgroundResource(R.drawable.textview_bg);
tv.setPadding(20, 5, 40, 5);
tv.setText("set your text");
tv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setTextColor(Color.RED);
tv.setClickable(true);
tv.setId(id);//where id is an integer which should be unique for each TextView

layout.addView(tv); 

您还需要在for循环中创建并添加所有这三个组件,根据迭代循环时使用的i提供唯一的ID!并且您可以为文本视图和按钮创建一个字符串数组,以便在for循环中设置它们的名称,这将更容易在循环中传递要设置的字符串。

您必须存储refe当您想要修改List.get(2).findViewById(R.id.textbox\u id)时,视图的长度会膨胀并存储在列表中


希望对您有所帮助。

您必须存储视图的引用并存储在列表中,然后才能修改simply List.get(2).findViewById(R.id.textbox\u id)


希望对您有所帮助。

谢谢您的回答。您能告诉我如何在布局充气机中找到单击的特定按钮……您可以为您的按钮设置标签()(根据num,例如
view.findViewById(R.id.buttonnid).setTag(新整数(num));
)充气时,在OnClickListener中获取此标记。
public void onClick(View v){Integer buttonNum=v.getTag();}
thanx…我对充气机还有一个疑问。是否可以一次性在水平和垂直方向创建充气视图
TextView tv=(TextView)视图。findViewById(R.id.TextView id);
谢谢你的回答。你能告诉我如何在布局充气机中找到一个特定的按钮……你可以为你的按钮设置标签()(根据num,例如
view.findViewById(R.id.buttonnid).setTag(新整数(num));
)充气时,在OnClickListener中获取此标记。
public void onClick(View v){Integer buttonNum=v.getTag();}
thanx…我对充气机还有一个疑问。是否可以一次性在水平和垂直方向创建充气视图
TextView tv=(TextView)视图。findViewById(R.id.TextView id);