2011-11-18 31 views
0

在我的应用程序活动时得到加载,我做NUM以下文本在一个TextView不改变,而这样做吹气布局

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

的值不同每次。

在我的LayoutInflater布局我有一个文本视图,编辑文本和一个按钮。

现在,他们根据num中提到的次数显示,现在每次我都希望改变文本和按钮的名称。如何设置TextView和Button的文本。

回答

2

只需设置他们,你吹的布局

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

感谢您的回答。你可以请我说如何找到一个特定的按钮被点击布局inflater ..... –

+1

你可以setTag()http://developer.android.com/reference/android/view/View.html# setTag(java.lang.Object)为您的按钮(根据num例如'view.findViewById(R.id.buttonid).setTag(new Integer(num));')当您膨胀它和OnClickListener时获取此标签。 'public void onClick(View v){Integer buttonNum = v.getTag();}' – Vladimir

+0

thanx ...我对Inflater有一个疑问。是否可以在同一时间在水平和垂直方向上创建充气视图 –

0

后,我认为它是什么,你应该创建一个包含只是一个LinearLayout中的XML文件。

然后编程,您应该创建TextViews,EditTexts和Buttons并将其添加到LinearLayout。

你可以像下面设置这些组件的不同属性:

这里是例如设置组件的TextView的properties.For休息的,你可以设置相同。

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循环,提供唯一的ID取决于我使用迭代循环!你可以有textviews和按钮设置一个String数组,其for循环中的名称,这会让您更容易将循环中要设置的字符串传递给它们。

0

您必须将视图的引用存储到一个List中,然后当你想修改list.get(2).findViewById(R.id.textbox_id)。

希望它有帮助。

相关问题