在XML中放比较固定的组件,在java代码中放变化比较多的复杂的组件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > </LinearLayout>
public class MainActivity extends Activity { private ImageView iView[] = new ImageView[2]; private int ImageID[] = new int[] { R.drawable.img01,R.drawable.img02 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout layout = (LinearLayout) findViewById(R.id.ll); LayoutParams params = new LayoutParams(253,148); for (int i = 0; i < ImageID.length; i++) { iView[i] = new ImageView(this); iView[i].setImageResource(ImageID[i]); iView[i].setPadding(5,5,5); iView[i].setLayoutParams(params); layout.addView(iView[i]); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main,menu); return true; } }
注意
1:java.lang.OutOfMemoryError错误,加载4张图片竟然还会说内存不足。。。
2 : setContentView(R.layout.activity_main);在findViewByID后面出现会导致得到的View是null