在运行时重复在Android浏览 [英] Duplicate Views on Android during Run-time
问题描述
我已经创建了一个活动布局文件。在这个布局我已创建了一个TextView和一个EditText一个LinearLayout中。 现在我想创造更多的LinearLayouts,将外观和包含我原来的LinearLayout,但不同的文本完全相同的看法。我也想运行期间以编程方式做到这一点,因为这些的LinearLayout量将运行之间有所不同。 我读过一些关于inflaters,但我不明白他们足够使用它们。
我想这样的事情,很明显,code是错误的,但希望你明白我想要做的:
的LinearLayout llMain =(的LinearLayout)findViewById(R.id.mainLayout);
的LinearLayout llToCopy =(的LinearLayout)findViewById(R.id.linearLayoutToCopy);
对于(INT球员= 0;玩家LT;大小;球员++)
{
的LinearLayout llCopy = llToCopy.clone();
TextView的电视=(TextView中)llCopy.getChildAt(0);
tv.setText(players.get(播放机).getName());
llMain.addView(llCopy);
}
有几种方法可以做到这一点。
一个快速简便的做法是夸大了新的布局,循环的每次迭代:
LayoutInflater吹气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
的LinearLayout父=(的LinearLayout)inflater.inflate(R.layout.main,NULL);
的for(int i = 0;我小于10;我++){
查看孩子= inflater.inflate(R.layout.child,NULL);
TextView的电视=(TextView中)child.findViewById(R.id.text);
tv.setText(儿童号+ I);
parent.addView(子);
}
的setContentView(父);
另一个(更优雅)解决方案是创建扩展的LinearLayout一个单独的类:
公共类ChildView扩展的LinearLayout {
私人TextView的电视;
公共ChildView(上下文的背景下){
超(上下文);
View.inflate(背景下,R.layout.child,这一点);
电视=(TextView中)findViewById(R.id.text);
}
公共无效的setText(字符串文本){
tv.setText(文本);
}
}
现在你可以在你的循环每次迭代创建一个 ChildView
,并设置通过的setText(字符串文本)的文本
方法:
的for(int i = 0;我小于10;我++){
ChildView孩子=新ChildView(本);
child.setText(儿童号+ I);
parent.addView(子);
}
I have created a Layout file for an activity. In this layout I have created a LinearLayout with a textview and an edittext. Now I want to create additional LinearLayouts that will look and contain the exact same views that my original LinearLayout, but with different texts. I also want to do it programmatically during run because the amount of these LinearLayout will vary between runs. I've read some about inflaters but I don't understand them enough to use them.
I'm thinking something like this, obviously the code is wrong, but hopefully you understand what I want to do:
LinearLayout llMain = (LinearLayout)findViewById(R.id.mainLayout);
LinearLayout llToCopy = (LinearLayout)findViewById(R.id.linearLayoutToCopy);
for(int player = 0; player < size; player++)
{
LinearLayout llCopy = llToCopy.clone();
TextView tv = (TextView)llCopy.getChildAt(0);
tv.setText(players.get(player).getName());
llMain.addView(llCopy);
}
There are several ways to accomplish this.
A quick and easy approach is to inflate a new layout in every iteration of the loop:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.main, null);
for (int i = 0; i < 10; i++) {
View child = inflater.inflate(R.layout.child, null);
TextView tv = (TextView) child.findViewById(R.id.text);
tv.setText("Child No. " + i);
parent.addView(child);
}
setContentView(parent);
Another (more elegant) solution is to create a separate class extending LinearLayout:
public class ChildView extends LinearLayout {
private TextView tv;
public ChildView(Context context) {
super(context);
View.inflate(context, R.layout.child, this);
tv = (TextView) findViewById(R.id.text);
}
public void setText(String text) {
tv.setText(text);
}
}
Now you can create a ChildView
in every iteration of your loop and set the text via the setText(String text)
method:
for (int i = 0; i < 10; i++) {
ChildView child = new ChildView( this );
child.setText("Child No. " + i);
parent.addView(child);
}
这篇关于在运行时重复在Android浏览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!