14

我已经编写了将图像加载到 ImageView 小部件中的代码:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);
    i = (ImageView)findViewById(R.id.imageView1);

    new get_image("https://www.google.com/images/srpr/logo4w.png") {
        ImageView imageView1 = new ImageView(GalleryActivity.this);

        ProgressDialog dialog = ProgressDialog.show(GalleryActivity.this, "", "Loading. Please wait...", true);

        protected void onPreExecute(){
             super.onPreExecute();
        }

         protected void onPostExecute(Boolean result) {
             i.setImageBitmap(bitmap); 
         dialog.dismiss();
         }
    }.execute();


}

bu 现在,我想加载几张图片。为此,我需要动态创建图像视图,但我不知道如何...

我想在 for 循环中运行我的代码:

for(int i;i<range;i++){
   //LOAD SEVERAL IMAGES. READ URL FROM AN ARRAY
}

我的主要问题是在循环内动态创建几个 ImageView

4

4 回答 4

28

您可以根据您的要求修改布局、图像资源和图像数量(也可以是动态的)...

LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
    ImageView image = new ImageView(this);
    image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
    image.setMaxHeight(20);
    image.setMaxWidth(20);

    // Adds the view to the layout
    layout.addView(image);
}
于 2013-03-12T08:52:14.650 回答
1

您可以使用此代码创建 ImageViews

ImageView image = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(vp);
image.setMaxHeight(50);
image.setMaxWidth(50);
// other image settings
image.setImageDrawable(drawable);
theLayout.addView(image);

其中 theLayout 是您要添加图像视图的布局。

如需更多定制,请查看开发页面,其中列出了所有可能的选项。

于 2013-03-12T08:41:08.443 回答
0

如果您的要求显示在列表或网格视图中,那么您应该选择延迟加载列表或网格。

请通过以下链接。

https://github.com/thest1/LazyList

https://github.com/abscondment/lazy-gallery

于 2013-03-12T08:36:00.583 回答
0

试试这个

 rootLayout = (LinearLayout) view1.findViewById(R.id.linearLayout1);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER_VERTICAL;

    params.setMargins(0, 0, 60, 0);


    for(int x=0;x<2;x++) {
        ImageView image = new ImageView(getActivity());

        image.setBackgroundResource(R.drawable.ic_swimming);
        rootLayout.addView(image);
    }
于 2018-02-08T20:35:22.893 回答