//手动添加imageview
RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.relaGameZhaiGuoZi);
imgApple2 = new ImageView(this);
imgApple2.setImageResource(R.drawable.app);
imgApple2.setId(110); //注意这点 设置id
imgApple2.setOnClickListener(this);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// lp1.addRule(RelativeLayout.ALIGN_TOP);
// lp1.setMargins(30, 50, 100, 100);//(int left, int top, int right, int bottom)
lp1.leftMargin=30;
lp1.topMargin = 100;
mainLayout.addView(imgApple2,lp1);Android 在已有布局中动态添加控件
最新推荐文章于 2025-10-13 09:30:59 发布
本文介绍了一种在Android应用中手动将ImageView添加到RelativeLayout的方法,包括设置图片资源、指定ID、添加点击监听器以及设置布局参数等关键步骤。
1450

被折叠的 条评论
为什么被折叠?



