添加ImageView的布局布局、ImageView

2023-09-07 03:18:12 作者:戒i

我要创建从code图像视图,并添加到布局...但我不使用IN-code设置layout.I有RelativeLayout的XML和使用的setContentView(R.layout.activity_game);如何布局添加ImageView的?

解决方案

  RelativeLayout的RL =(RelativeLayout的)findViewById(R.id.id_of_your_relative_layout_here);ImageView的IV =新ImageView的(这一点)iv.setImageResource(R.drawable.some_drawable_of_yours); //或iv.setImageDrawable(getResources()getDrawable(R.drawable.some_drawable_of_yours));rl.addView(四); 

I want to create image view from code and add to layout...But l don't use in-code setup layout.I have RelativeLayout in xml and use that setContentView(R.layout.activity_game); How add imageview to layout?

解决方案 在Qt界面已存在的布局中添加新的控件

RelativeLayout rl = (RelativeLayout)findViewById(R.id.id_of_your_relative_layout_here);
ImageView iv = new ImageView(this)
iv.setImageResource(R.drawable.some_drawable_of_yours); //or iv.setImageDrawable(getResources().getDrawable(R.drawable.some_drawable_of_yours));
rl.addView(iv);