android 使用代码和xml配合进行布局

 最开始习惯使用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中只是一个简单的现行布局

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值