Android从drawable中动态获取图片设置为XXXLayout背景

来源:互联网 发布:施耐德plc编程电缆 编辑:程序博客网 时间:2024/05/02 01:58

你drawable中的图片文件是统一规律命名的可以通过下面代码获取到

setImageResource(getResources().getIdentifier("文件名_" + "索引号", "drawable", "com.xxxx.xxxxx")); 

索引号可以通过指定上下限的random获取

Random random = new Random();String num = Integer.toString(random.nextInt(3));

然后就是是获取到当前activity的layout布局文件,给他设置背景

getWindow().getDecorView().setBackgroundResource(getResources().getIdentifier("splash" + num, "drawable", "com.wmh.colorfuldays"));