带有图像和边距的 for 循环

for loop with Image and margin

我的 for 循环/它的内容有问题。
我想要这个循环,其中图像重复 n 次。 此外,这些图像的顶部应该有边距,这样它们之间就有一些 space。

目前这些图像彼此重叠或不会按应有的方式生成(结果:Drawable“bg_circle”仅显示一次).

userinput”将从警报对话框中的 EditText 中填充。
这是我的代码:

int n = Integer.parseInt(userInput.getText().toString());
RelativeLayout layout = findViewById(R.id.TableView);
        for(int i = 0; i <= n; i++){
           ImageView image = new ImageView(mContext);
           image.setImageResource(R.drawable.bg_circle);
           RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
           lp.setMargins(100,100,0,0);
           lp.height = 100;
           lp.width = 100;
           image.setLayoutParams(lp);
           layout.addView(image);
        }

哪里错了?

使用垂直方向的 LinearLayout 而不是 RelativeLayout。此外,您应该使用 ScrollView 作为布局的根元素:

<ScrollView ...> <LinearLayout android:orientation="vertical" ...> ... programmatically added images... </LinearLayout> </ScrollView>