最开始习惯使用XML来进行布局,看疯狂讲义上面的用代码配合布局用着也还好,分享给大家
public class MainActivity extends Activity {
int currentpage=0;
int[] image=new int []{
R.drawable.aaa,
R.drawable.bbb,
R.drawable.ccc,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout=(LinearLayout) findViewById(R.id.lala);
final ImageView imageView=new ImageView(this);
layout.addView(imageView);
imageView.setImageResource(image[0]);
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if ( currentpage >=2 ) {
currentpage = -1;
}
imageView.setImageResource(image[++currentpage]);
}
});
}
@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;
}
}
这是一个点击图片求换到下一张的demo,main.XML中只是一个简单的现行布局