android - studio - relativelayout import




添加imageview到佈局 (2)

XML應該是:

<?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>

代碼(在onCreate內):

RelativeLayout rl = (RelativeLayout)findViewById(R.id.relativeblablabla);
ImageView imageView = new ImageView (this);
iv.setImageResource(R.drawable.YOUR_IMAGE);
rl.addView(imageView);

我想從代碼創建圖像視圖,並添加到佈局...但我不使用代碼內安裝layout.I在xml中有RelativeLayout並使用setContentView(R.layout.activity_game); 如何添加imageview佈局?


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);




relativelayout