使用LayoutInflater创建动态视图 [英] Using LayoutInflater to create dynamic view
问题描述
我创建了一个XML文件,我想重复的次数和主视图内填充。
我已阅读,LayoutInflater是要走的路,但我有得到它的工作的问题。
I have created a xml file I wish to repeat a number of times and populate within the main view. I have read that LayoutInflater is the way to go but I am having issues getting it to work
第一个问题。我创建的XML项目拥有一批在它的TextView的。可以这样做,也可以在XML只包含一个TextView的?
first question. the xml item I created has a number of textview within it. Can this be done or can the xml only contain one textview?
ScrollView mainlayout = (ScrollView) findViewById(R.id.ScrollView1);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(R.layout.sightmarks_item, null);
TextView textview1 = (TextView) inflater.inflate(R.id.textView1, null);
EditText editview1 = (EditText) inflater.inflate(R.id.editview1, null);
EditText editview2= (EditText) inflater.inflate(R.id.editview2, null);
我然后运行轮为一个阵列上循环,并设置每个值文本上方,然后加入视图到mainlayout视图
I am then running round a for loop on an array and setting the text for each the values above and then adding the view to the mainlayout view
目前它示数在膨胀的看法。
我在做正确吗?
Currently it is erroring on the inflated view. am I doing this correctly?
感谢
推荐答案
有在你的code一些问题。为了得到任何视图使用的元素 findViewById
法
There is some problem in your code. To get the element of any view use findViewById
method
ScrollView mainlayout = (ScrollView) findViewById(R.id.ScrollView1);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(R.layout.main, null);
TextView textview1 = (TextView) view.findViewById(R.id.textview1);
EditText editview1 = (EditText) view.findViewById(R.id.editview1);
EditText editview2= (EditText) view.findViewById(R.id.editview2);
这篇关于使用LayoutInflater创建动态视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!