Android 将imageview添加到布局中

Android 将imageview添加到布局中,android,imageview,android-relativelayout,Android,Imageview,Android Relativelayout,我想从代码中创建图像视图并添加到布局中…但我不在代码设置布局中使用。我在xml中有RelativeLayout并使用setContentView(R.layout.activity\u game); 如何将imageview添加到布局?XML应该是: RelativeLayout rl = (RelativeLayout)findViewById(R.id.id_of_your_relative_layout_here); ImageView iv = new ImageView(this)

我想从代码中创建图像视图并添加到布局中…但我不在代码设置布局中使用。我在xml中有RelativeLayout并使用
setContentView(R.layout.activity\u game);
如何将imageview添加到布局?

XML应该是:

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);
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:id="@+id/relativeblablabla"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

...........YOUR CODE HERE............

</RelativeLayout>

我找不到布局的id?@Filip您的
RelativeLayout
id是什么?您可以在XML中设置它,比如
@Filip对不起,使用rl.addView(iv);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.relativeblablabla);
ImageView imageView = new ImageView (this);
iv.setImageResource(R.drawable.YOUR_IMAGE);
rl.addView(imageView);