我想通过ForLoop动态地将几个自定义ViewGroup添加到现有的RelativeLayout。将自定义ViewGroup添加到RelativeView
当我添加它们时,不显示任何内容。也许这是因为ViewGroup在添加之前没有完成加载?
无论如何解决这个问题?
谢谢
GameActivity.java:
hScrollItems = (RelativeLayout) findViewById(R.id.hScrollItems);
for (int i = 0; i < 5; i++){
int w = getResources().getInteger(R.integer.GridItemWidth);
int h = getResources().getInteger(R.integer.GridItemWidth);
GridItem gi = new GridItem(getBaseContext(), w, h, GridType.EMPTY);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.setMargins(w * i, 0, 0, 0);
hScrollItems.addView(gi, params);
}
GridItem.java
public GridItem(Context context, int Width, int Height, GridType Type) {
super(context);
/*LayoutParams params = new LayoutParams(Width, Height);
this.setLayoutParams(params);*/
this.setBackgroundColor(0xFFFFFF33);
ImageView img = new ImageView(context);
img.setBackgroundResource(R.drawable.ic_launcher);
this.addView(img);
}
public enum GridType {
EMPTY("EMPTY"),
HOUSE("HOUSE");
private String value;
private GridType(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
我试过改变它,但没有结果,仍然没有显示。 – Tom
在答案中增加了更多 – Geobits
我已经尝试为每个GridItem设置ID并添加hScrollItems.requestLayout(),但仍然没有显示...... :( – Tom