我正在尝试通过 ForLoop 将几个自定义 ViewGroup 动态添加到现有的 RelativeLayout。
当我添加它们时,什么都没有显示。也许是因为 ViewGroup 在添加之前没有完成加载?
有任何解决这个问题的方法吗?
谢谢,
游戏活动.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;
}
}