main.xml....................
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:id="@+id/layout"
>
activi..............................
package com.example.ui;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private ImageView [] img=new ImageView[4]; //声明保存一个ImageView组件的数组
private int[] imagePath=new int[]{ //声明并初始化一个保存访问图片的数组
R.drawable.j1,R.drawable.j2,R.drawable.j3,R.drawable.w1
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout=(LinearLayout)findViewById(R.id.layout);//获取个xml文件中定义的线性布局管理器
for(int i=0;i
img[i]=new ImageView(this);//创建一个ImageView组件
img[i].setImageResource(imagePath[i]);//为ImageView组件指定要显示的图片
img[i].setPadding(5, 5,5, 5);//设置ImageView组件的内边距
LayoutParams params=new LayoutParams(253,148);//设置图片的宽度和高度
img[i].setLayoutParams(params);//为ImageView组件设置布局参数
layout.addView(img[i]);//将ImageView组件添加到布局管理器中
}
}
@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;
}
}
效果如下图: