如何在android中自动创建imageview

如何在android中自动创建imageview,android,imageview,Android,Imageview,我的android项目已连接到服务器。我从服务器检索数据(如果是5),然后在linearlayout中创建5个imageview。换句话说,自动创建imageview取决于检索到的数据。此代码循环检查图像数量,每次创建新的imageview,然后将它们添加到父线性布局中。您还可以动态设置ImageView的LayoutParams LinearLayout layout = (LinearLayout)findViewById(R.id.parentLayout); for(int i=0;i&

我的android项目已连接到服务器。我从服务器检索数据(如果是5),然后在linearlayout中创建5个imageview。换句话说,自动创建imageview取决于检索到的数据。

此代码循环检查图像数量,每次创建新的imageview,然后将它们添加到父线性布局中。您还可以动态设置ImageView的LayoutParams

LinearLayout layout = (LinearLayout)findViewById(R.id.parentLayout);
for(int i=0;i<number_of_images;i++) {
    ImageView image = new ImageView(context);
    layout.addView(image);
}
LinearLayout布局=(LinearLayout)findViewById(R.id.parentLayout);
对于(int i=0;i
线性布局:
LinearLayout LinearLayout=新的LinearLayout(本);
linearLayout.setOrientation(linearLayout.VERTICAL);
linearLayout.setLayoutParams(新的LayoutParams(LayoutParams.MATCH_父级,LayoutParams.MATCH_父级));

对于(int i=0;iThanks如何添加到水平视图中取决于您的父布局。如果您的父布局的方向是
android:orientation=“Horizontal”
,则它将水平添加子布局。感谢您的帮助
For Linear Layout:

LinearLayout linearLayout= new LinearLayout(this);
                 linearLayout.setOrientation(LinearLayout.VERTICAL);
                 linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
for(int i=0;i<Number_Images;i++){
//ImageView Setup
ImageView i mageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
}
//make visible to program


setContentView(linearLayout);