我在android中创建简单的记事本应用程序,在选项菜单中我有"更改背景"作为一个菜单项,当用户点击它时,我只想更改背景图像[例如,我想将我的背景图像从efil塔更改为tajmahal],我有10个图像在drawable ..如何随机更改它当每次用户点击"更改背景" option.Thank you。
答案 0 :(得分:0)
int images[] = {R.drawable.eifel,R.drawable.tajmahal};
LinearLayout backLayout;
onCreate(Bundle savedInstanceState) {
backLayout = (LinearLayout) findViewById(R.id.mybackgroundlayout);
}
private void changeBackground(){
backLayout.setBackgroundResource(images[randInt(0,images.length-1)]);
}
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.changebackground:
changeBackground();
break;
}
return true;
}