如何在一个 activity 上随机设置多个背景

how to setup multiple background on one activity randomly

我需要知道如何在一个 activity 上随机设置背景, 'android:background="@drawable/backG"' 只显示一张图片 背景随着应用程序的启动而随机出现,并一直显示直到用户退出应用程序,当他重新打开它时,它会显示一个新的背景 注意:我的项目中只有一个 activity 提前致谢

我想,你应该使用 Java 代码来制作这个。
例如,您的 RelativeLayout 是 activity
中的主要布局 你应该使用

RelativeLayout rLayout = (RelativeLayout) findViewById (R.id.rLayout);
Resources res = getResources(); //resource handle
Drawable drawable = res.getDrawable(R.drawable.newImage); //new Image that was added to the res folder
rLayout.setBackground(drawable);

对于随机背景:您创建一个包含所有图像背景的数组 并使用下面的代码随机位置

Random randomGenerator = new Random();
int randomPositionInt = randomGenerator.nextInt(10); // it will generate position in 0 - 10

希望对您有所帮助