【发布时间】:2015-09-24 23:51:56
【问题描述】:
我需要知道如何在一项活动中随机设置背景, 'android:background="@drawable/backG"' 只显示一张图片 背景随着应用程序的启动随机出现,并一直显示,直到用户退出应用程序,当他重新打开应用程序时,它将显示一个新的背景 注意:我的项目中只有一项活动 提前致谢
【问题讨论】:
标签: java android xml background launcher
我需要知道如何在一项活动中随机设置背景, 'android:background="@drawable/backG"' 只显示一张图片 背景随着应用程序的启动随机出现,并一直显示,直到用户退出应用程序,当他重新打开应用程序时,它将显示一个新的背景 注意:我的项目中只有一项活动 提前致谢
【问题讨论】:
标签: java android xml background launcher
我认为,您应该使用 Java 代码来完成此操作。
例如,您有 RelativeLayout 是您活动中的主要布局
你应该使用
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
希望有帮助
【讨论】: