我想通過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