<strong><span style="font-size:18px;">先封装方法</span></strong>
public class OperateAssets {

private static OperateAssets operateAssets;
public static OperateAssets getInstance(){
if(operateAssets == null){
operateAssets = new OperateAssets();
}
return operateAssets;
}

/*
* 获取Assets文件夹下的图片资源
*/
public Bitmap getImageFromAssetsFile(Context context, String path) {
Bitmap bitmap = null;
AssetManager assetManger = context.getResources().getAssets();
try {
InputStream inputStream = assetManger.open(path);
bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}

/*
* 将bitmap转成Drawable
*/
public Drawable convertBitmapToDrawable(Bitmap bitmap,Resources res){
return new BitmapDrawable(res, bitmap);
}
}


然后把资源载入布局

Bitmap bitmap = OperateAssets.getInstance().getImageFromAssetsFile(getActivity(), "themediy/skin_chat_background.png");
RelativeLayout relativeLayout = (RelativeLayout)view.findViewById(R.id.message_bg);
relativeLayout.setBackgroundDrawable(OperateAssets.getInstance().convertBitmapToDrawable(bitmap, getResources()));

我这里用的是fragment

对相对布局设置背景