以编程方式Android创建ImageView数组

时间:2016-04-23 21:53:49

标签: android

我的活动包含String数组,每个String都是drawable图像名称,例如,如果我有一个名为" abcd"然后在String数组中,第一个值将是" abcd"。我试图将此String数组传递给另一个活动,然后显示所有图像。

第一项活动:

            Intent ip = new Intent(main3.this, main4.class);
            ip.putExtra("p", usedcards1);
            startActivity(ip);

第二项活动:

        ImageView[] usedcardss;
        RelativeLayout rel = (RelativeLayout) findViewById(R.id.main4con);
        usedcardss = new ImageView[8];
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        for (int i = 0; i < usedcardss.length; i++)
            usedcardss[i] = new ImageView(this);
        Intent pp = this.getIntent();
        String[] cards = pp.getStringArrayExtra("p");
        for (int i = 0; i < 8; i++) {
            int icon = getResources().getIdentifier(cards[i], "drawable", getPackageName());
            usedcardss[i].setImageResource(icon);
            usedcardss[i].setLayoutParams(lp);
            rel.addView(usedcardss[i]);
        }

创建了一个新活动,但它只是空白。为什么呢?

1 个答案:

答案 0 :(得分:1)

可能与您的问题没有直接关系,但是您使用的是相对布局,我在您的代码中没有看到您正在设置图像布局参数,这会导致imageViews重叠