【问题标题】:Dynamically added images not showing动态添加的图片不显示
【发布时间】:2012-11-20 03:22:35
【问题描述】:

在这段代码中,我正在构建一个方法,该方法在运行时将同一图像多次添加到相对布局中。但是,调试时没有显示图像。添加的文本视图运行正常。有人可以解释如何使图像可见吗?提前致谢。

我的代码:

public class TestActivity extends Activity {
ArrayClass arraysObject1 = new ArrayClass();
ArrayList<ImageView> mImages = new ArrayList<ImageView>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RelativeLayout layout = new RelativeLayout(this);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
        layout.setLayoutParams(params);
        layout.setBackgroundColor(Color.parseColor("#FFFFFF"));

        //textview is working
        TextView testText = new TextView(this);
        testText.setText("Alle aangeboden vacatures vindt u hieronder terug.");
        testText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    layout.addView(testText);


        RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        imgParams.addRule(RelativeLayout.CENTER_IN_PARENT);

        for(int i=0; i<arraysObject1.array1.size(); i++)
        {
            mImages.add(new ImageView(this));
            mImages.get(i).setVisibility(View.VISIBLE);
            mImages.get(i).setBackgroundResource(R.drawable.work);
            mImages.get(i).setLayoutParams(imgParams);

            layout.addView(mImages.get(i));

        }
        setContentView(layout);
    }
}

【问题讨论】:

  • 太多ImageViews 可能会抛出OutOfMemoryException。我猜:)

标签: android image dynamic


【解决方案1】:

您的循环for(int i=0; i &lt; arraysObject1.array1.size(); i++) 似乎没有执行。 arraysObject1.array1 为空。

【讨论】:

  • 你是对的!我没有考虑到这种可能性。谢谢。
  • 我也经常犯这样的错误:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
  • 2015-12-16
  • 2017-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多