设置背景信息:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
这是最好的方法吗?
设置背景信息:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
这是最好的方法吗?
当前回答
你也可以设置任何图片的背景:
View v;
Drawable image=(Drawable)getResources().getDrawable(R.drawable.img);
(ImageView)v.setBackground(image);
其他回答
你也可以设置任何图片的背景:
View v;
Drawable image=(Drawable)getResources().getDrawable(R.drawable.img);
(ImageView)v.setBackground(image);
RelativeLayout relativeLayout; //declare this globally
现在,在任何函数中,如onCreate, onResume
relativeLayout = new RelativeLayout(this);
relativeLayout.setBackgroundResource(R.drawable.view); //or whatever your image is
setContentView(relativeLayout); //you might be forgetting this
在app/res/your_xml_layout_file.xml中
为父布局分配一个名称。 去MainActivity,通过调用findViewById(R.id."given_name")找到RelativeLayout。 通过调用setBackgroundColor()方法,将布局作为经典对象使用。
使用butterknife将可绘制资源绑定到变量,方法是将其添加到类的顶部(在任何方法之前)。
@Bind(R.id.some_layout)
RelativeLayout layout;
@BindDrawable(R.drawable.some_drawable)
Drawable background;
然后在其中一个方法中添加
layout.setBackground(background);
这就是你所需要的
setBackground(getContext().getResources().getDrawable(R.drawable.green_rounded_frame));