主Activity中,把GameLayout  加入到主布局中,
ViewGroup parent = xxx
parent.addView(GameLayout,LayoutParams()); //这个是fill_parent,
如果要加点空闲就用marging

解决方案 »

  1.   

    楼主的addView()使用带LayoutParams的参数试试,我这里大概帮你写了下,可能楼主还需要调试一下。public class GameLayout extends LinearLayout{ public GameLayout(Context context,ImageView[][] images) {
    super(context);
    setLayout(context,images);
    setBackgroundColor(Color.parseColor("#4682B4"));
    } private void setLayout(Context context, ImageView[][] images) {
    // TODO Auto-generated method stub
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
    LinearLayout linearLayout=new LinearLayout(context);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
                    linearLayout.setLayoutParams(lp);
    linearLayout.setGravity(LinearLayout.TEXT_ALIGNMENT_CENTER);
    linearLayout.setPadding(0, 0, 0, 0);
    for (int i = 0; i < images.length; i++) {
    LinearLayout liner = new LinearLayout(context);
    liner.setOrientation(LinearLayout.HORIZONTAL);
    int leng=images[i].length;
    for (int j = 0; j < leng; j++) {
    ImageView img=(ImageView)images[i][j];
    liner.addView(img);
    }
    linearLayout.addView(liner);
                            liner.addView(img, lp);
    liner=null;
    }
    this.addView(linearLayout, lp);
    }}第二个也是如此,带LayoutParams,可能需要LinearLayout的也可能需要ViewGroup,看会不会报错。