【问题标题】:Create ImageViews dynamically inside a loop在循环内动态创建 ImageView
【发布时间】:2013-02-27 16:27:00
【问题描述】:

我已经编写了将图像加载到 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();


}

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

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

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

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

【问题讨论】:

  • 无法使用Android内置列表控件有什么特殊原因吗?
  • @Premsuraj:不,我可以为此使用内置小部件吗?怎么样?
  • 为什么需要多个图像视图?目的是什么?您需要在列表中显示图像吗?
  • @vmerror:我想在像 instagram 应用这样的滚动查看器中加载几张图像。请以简单的方式帮助我
  • 如果有要求,你需要先学习android ListView 和自定义Adapter,在adapter getView 方法中下载图片并设置为自定义xml 布局。这应该是方法。

标签: java android imageview


【解决方案1】:

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

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);
}

【讨论】:

  • 嗨,侯赛因。为什么要设置 setMaxHeight 和 setMaxWidth?
  • 我们可以为该图像视图设置边距吗?
  • 是的,我们已经为此设置了布局参数,然后我们可以设置边距,关注link
  • 我的 LinearLayout 在 Horizo​​ntalScrollView 中。在 LinearLayout 中,我希望在垂直方向上组合 ImageView 和 TextView。而且我想增加许多这样的组合而不是单个 ImageView。另外,我想以编程方式为该图像和文本视图设置我的样式。
【解决方案2】:

您可以使用此代码创建 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 是您要添加图像视图的布局。

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

【讨论】:

    【解决方案3】:

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

    请通过以下链接。

    https://github.com/thest1/LazyList

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

    【讨论】:

    • 我想在滚动查看器中显示几张图片。像 Instagram 应用程序
    【解决方案4】:

    试试这个

     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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2014-11-06
      • 2012-10-16
      • 1970-01-01
      • 2015-06-25
      相关资源
      最近更新 更多