我正在编写一个应用程序,为每个找到的文件加载一行按钮(下面是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);
}
答案 0 :(得分:0)
老实说,在代码中执行所有操作似乎有些过分。我建议创建一个布局文件,并多次充气。使appLayout成为一个垂直方向的LinearLayout,它就像这样简单:
for(int x = 1; x <= 15; x++){
View view = LayoutInflater.from(this).inflate(R.layout.some_layout, appLayout, false);
// Configure the items inside the view
Button button1 = (Button)view.findViewById(R.id.button1);
button1.setText("Button 1");
button1.setOnClickListener(new View.OnClickListener() { ... });
...
appLayout.addView(view);
}
你的布局文件也会相对简单:
<LinearLayout android:orientation="vertical" ...>
<LinearLayout android:orientation="horizontal" ...>
<Button android:id="@+id/button1" ... />
<Button android:id="@+id/button2" ... />
</LinearLayout>
<View android:background="@android:color/black"
android:layout_height="3dp"
android:layout_width="match_parent" />
</LinearLayout>
虽然在代码中布置元素绝对是可行的,但要想做到这一点可能会令人头痛。我发现使用XML文件更容易,并根据需要对它们进行充气。我的猜测是你使用你的ID的方式混淆了布局。最好让按钮获得自己的id(不要使用setId),并在设置相对布局参数时使用“getId”方法。