【发布时间】:2015-11-24 22:26:36
【问题描述】:
我正在编写一个应用程序,它为找到的每个文件加载一排按钮(2 个按钮,然后是下面的黑线)(因此循环计数不会是静态的)。目前,在构建时,我的静态循环计数为 15。但是在运行代码时,它会在左侧创建较大的按钮,并且在下方创建黑线……但是……右侧的较小按钮仅出现一次。知道为什么吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scrollPictures = new ScrollView(this);
RelativeLayout appLayout = new RelativeLayout(this);
// appLayout.setClipBounds(null);
Resources r = getResources();
ImageView blackLine;
RelativeLayout.LayoutParams p;
int id = 1;
for(int x = 1; x <= 15; x++){
p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Button studentsButton = new Button(this);
studentsButton.setClipBounds(null);
studentsButton.setId(id);
studentsButton.setHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
studentsButton.setWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
//studentsButton.setBackgroundColor(Color.LTGRAY);
if (x > 1 ){
p.addRule(RelativeLayout.BELOW, id - 1);
studentsButton.setLayoutParams(p);
}
appLayout.addView(studentsButton);
id ++;
p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Button soundButton = new Button(this);
soundButton.setClipBounds(null);
soundButton.setHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
soundButton.setWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
//soundButton.setBackgroundColor(Color.LTGRAY);
p.addRule(RelativeLayout.RIGHT_OF, id - 1);
soundButton.setLayoutParams(p);
appLayout.addView(soundButton);
p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
blackLine = new ImageView(this);
blackLine.setId(id);
blackLine.setBackgroundColor(Color.BLACK);
blackLine.setMinimumWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
blackLine.setMinimumHeight(3);
blackLine.setMinimumWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
blackLine.setClipBounds(null);
p.addRule(RelativeLayout.BELOW, id - 1);
blackLine.setLayoutParams(p);
appLayout.addView(blackLine);
id++;
}
scrollPictures.addView(appLayout);
setContentView(scrollPictures);
}
【问题讨论】:
标签: java android layout screen