动态生成Layout的问题
无法使生成的layout适应大部分屏幕,屏幕变大就有很多空隙。GameLayout
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 linearLayout=new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
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=null;
}
this.addView(linearLayout);
}
}
setLayout
--------------------编程问答-------------------- 主Activity中,把GameLayout 加入到主布局中,
private void setLayout() {
// TODO Auto-generated method stub
GameLayout lay=new GameLayout(this, mImages);
lay.setOrientation(LinearLayout.VERTICAL);
lay.setGravity(Gravity.CENTER_HORIZONTAL);
setContentView(lay);
Button showImageButton=new Button(this);;
showImageButton.setOnClickListener(new ShowImageBtnClick());
showImageButton.setBackgroundResource(R.drawable.showimg);
LinearLayout liner = new LinearLayout(this);
liner.setOrientation(LinearLayout.HORIZONTAL);
liner.setGravity(Gravity.CENTER_HORIZONTAL);
showImageButton.setWidth(200);
liner.addView(showImageButton);
lay.addView(liner);
}
ViewGroup parent = xxx
parent.addView(GameLayout,LayoutParams()); //这个是fill_parent,
如果要加点空闲就用marging --------------------编程问答-------------------- 楼主的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,看会不会报错。
--------------------编程问答--------------------
都写了 没有报错 也没有效果。 --------------------编程问答-------------------- 遇到同样的问题,学习学习 --------------------编程问答-------------------- 来学习学习。。。。。。 --------------------编程问答-------------------- 没人。。唉~~~
补充:移动开发 , Android