如何在运行时生成图像视图

时间:2014-01-23 06:49:56

标签: android imageview parse-platform

我想在运行时在ImageViews上显示图像,并从Parse.com获取图像。我能够使用我的代码中预定义的ImageView数组显示图像。喜欢:

ImageView ad1,ad2,ad3,ad4,ad5,ad6;
private ImageView[] imgs = new ImageView[5];
ad1=(ImageView) findViewById(R.id.ad1);
    ad2=(ImageView) findViewById(R.id.ad2);
    ad3=(ImageView) findViewById(R.id.ad3);
    ad4=(ImageView) findViewById(R.id.ad4);
    ad5=(ImageView) findViewById(R.id.ad5);
    ad6=(ImageView) findViewById(R.id.ad6);
     imgs[0] = ad2; 
     imgs[1] = ad3; 
     imgs[2] = ad4; 
     imgs[3] = ad5; 
     imgs[4] = ad6;
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Footer");
     query.orderByDescending("updatedAt");
     query.whereEqualTo("Status", true);
        ob = query.find();
     for (ParseObject country : ob) {
         ParseFile image = (ParseFile) country.get("imageFile");
         imgl.DisplayImage(image.getUrl(), imgs[i]);
        i=i+1;
         System.out.println("the urls are"+image.getUrl());

              }
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

XML布局:

 <HorizontalScrollView
 android:id="@+id/horizontalScrollView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
 <LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:orientation="horizontal" >

 <ImageView
     android:id="@+id/ad2"
     android:layout_width="90dp"
     android:layout_height="wrap_content"
     android:layout_margin="3dp"
     tools:ignore="ContentDescription" />

    <ImageView
        android:id="@+id/ad3"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        tools:ignore="ContentDescription" />

    <ImageView
        android:id="@+id/ad4"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        tools:ignore="ContentDescription" />

    <ImageView
        android:id="@+id/ad5"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        tools:ignore="ContentDescription" />

     <ImageView
         android:id="@+id/ad6"
         android:layout_width="90dp"
         android:layout_height="wrap_content"
         android:layout_margin="3dp"
         tools:ignore="ContentDescription" />

    </LinearLayout>
</HorizontalScrollView>
</LinearLayout>

有没有什么方法可以让我不必对ImageView进行硬编码,它会自动生成parseobjects的数量?感谢

3 个答案:

答案 0 :(得分:1)

LinearLayout container=(LinearLayout) findViewById(R.id.container);

ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Footer");
     query.orderByDescending("updatedAt");
     query.whereEqualTo("Status", true);
        ob = query.find();
     for (ParseObject country : ob) {
         ParseFile image = (ParseFile) country.get("imageFile");
         ImageView iv=new ImageView(this);
         imgl.DisplayImage(image.getUrl(), iv);
          container.addView(iv);
        i=i+1;
         System.out.println("the urls are"+image.getUrl());

              }
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

还设置LinearLayout的id并删除所有其他ImageView:

<HorizontalScrollView
 android:id="@+id/horizontalScrollView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
 <LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:orientation="horizontal" android:id="@+id/container">


    </LinearLayout>
</HorizontalScrollView>

答案 1 :(得分:1)

在xml文件中,删除ImageView。并给linearLayout一些id。

<HorizontalScrollView
 android:id="@+id/horizontalScrollView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
 <LinearLayout
 android:id="@+id/linearLayout"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>

在您的活动中,定义此线性布局。

//像这样的东西

private LinearLayout linearLayout;

然后,在onCreate

linearLayout = (LinearLayout) findViewById(R.id.linearLayout);

然后在for循环中(ParseQuery一个),

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(20, 10, 20, 10); //optional you can also set more margins and do lot of stuff here
ImageView imageView = new ImageView(context); //context is the activity context say, this
imageView.setLayoutParams(lp);
imgl.DisplayImage(image.getUrl(), imageView);
linearLayout.addView(imageView);

答案 2 :(得分:0)

ImageView iv = new ImageView(context_here);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.wrap_content,LinearLayout.LayoutParams.wrap_content);
iv.setLayoutParams(params);
ll_your_main_container.addView(iv);

调用这是将ImageView个对象添加到主布局的循环。