android LinearLayout 动态设置背景图片

LinearLayout iv = new LinearLayout(activity);
iv.setBackgroundColor(resources.getColor(R.color.calendar_zhe_day));
这是设置背景颜色的代码,我想换成drawable文件夹下的title_btn.png图片要如何写代码呢???

代码如下:

LinearLayout temp=(LinearLayout)findViewById(R.id.LinearLayout的id);
Drawable d=Drawable.createFromPath(图片路径);
temp.setBackgroundDrawable(d);
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-01
方法一:iv.setBackgroundResource(R.drawable.title_btn)
方法二:iv.setBackgroundDrawable(getResources().getDrawable(R.drawable.title_btn));本回答被提问者采纳
相似回答