动态添加未显示的图像

时间:2012-12-02 14:11:53

标签: android image dynamic

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

我的代码:

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

1 个答案:

答案 0 :(得分:0)

您的周期for(int i=0; i < arraysObject1.array1.size(); i++)似乎没有被执行。 arraysObject1.array1是空的。