以编程方式创建 ImageView 数组 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]);
        }

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

也许与您的

问题没有直接关系,但您使用的是相对布局,并且在代码中看不到您正在设置图像布局参数的位置,因此会导致 imageViews 重叠

最新更新