我必须以编程方式添加图像,但是,我试图复制并粘贴相同的代码,但使用不同的图像文件。这不起作用,相反它只是选择了最后一张图片"添加"。 这是我添加一张图片的代码:
String flag = getIntent().getStringExtra("IMAGE");
int id = getResources().getIdentifier(flag, "drawable", getPackageName());
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
ImageView imageView = new ImageView(this);
imageView.setImageResource(id);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(imageView);
setContentView(linearLayout);
这是我的#34;复制并粘贴":
String pic = getIntent().getStringExtra("IMAGECITY");
int id1 = getResources().getIdentifier(pic, "drawable", getPackageName());
LinearLayout linearLayout1= new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
linearLayout1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
ImageView imageView1 = new ImageView(this);
imageView1.setImageResource(id1);
imageView1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout1.addView(imageView1);
setContentView(linearLayout1);
任何人都可以帮我解决添加另一张图片并将其放在底部吗?
答案 0 :(得分:0)
在Selvin的帮助下,我删除了:
LinearLayout linearLayout1= new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
linearLayout1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
并使用linearLayout1将变量更改为linearLayout。