gpt4 book ai didi

android - 如何在运行时生成 ImageView

转载 作者:行者123 更新时间:2023-11-29 21:16:33 26 4
gpt4 key购买 nike

我想在运行时在 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>

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

最佳答案

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 并删除所有其他 ImageViews:

<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>

关于android - 如何在运行时生成 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21301276/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com