android setBackgroundDrawable不工作

android setBackgroundDrawable不工作,android,Android,这个12小时调试..帮帮我 资源/可提取 main_img1.png main_img2.png xml 成员 RelativeLayout relativelayout; 一次创建 relativelayout = new RelativeLayout(this); relativelayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_img2));

这个12小时调试..帮帮我

资源/可提取

    main_img1.png
    main_img2.png
xml

成员

    RelativeLayout relativelayout;
一次创建

    relativelayout = new RelativeLayout(this);  
    relativelayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_img2));
    relativelayout.invalidate();
relativelayout = new RelativeLayout(this); 
relativelayout = (RelativeLayout)findViewById(R.id.your_layout)
  RelativeLayout relativelayout;
relativelayout = (RelativeLayout)findViewById(R.id.your_layout)
    relativelayout.setBackgroudResource(null);
    relativelayout.setBackgroundResource(R.drawable.main_img2);
形象是不变的 setBackgroundDrawable不工作 没有错误,没有红线

您不能使用imageview,
动态更改为布局的背景,即处理程序


请分享您的知识

您需要让亲属知道您所指的资源:

xml

一次创建

    relativelayout = new RelativeLayout(this);  
    relativelayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_img2));
    relativelayout.invalidate();
relativelayout = new RelativeLayout(this); 
relativelayout = (RelativeLayout)findViewById(R.id.your_layout)
  RelativeLayout relativelayout;
relativelayout = (RelativeLayout)findViewById(R.id.your_layout)
    relativelayout.setBackgroudResource(null);
    relativelayout.setBackgroundResource(R.drawable.main_img2);

我认为问题在于您的可绘制性缺少其固有的界限。因此,使用
setBackgroundResource
并让视图自动调整边界,而不是
setBackgroundDrawable

比如说

relativelayout.setBackgroundResource(R.drawable.main_img2);
请试试这个:

xml:

一次创建

    relativelayout = new RelativeLayout(this);  
    relativelayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_img2));
    relativelayout.invalidate();
relativelayout = new RelativeLayout(this); 
relativelayout = (RelativeLayout)findViewById(R.id.your_layout)
  RelativeLayout relativelayout;
relativelayout = (RelativeLayout)findViewById(R.id.your_layout)
    relativelayout.setBackgroudResource(null);
    relativelayout.setBackgroundResource(R.drawable.main_img2);
我希望它对你有用。

这样做

XML:


不推荐使用setBackgroundDrawable方法。改用

setBackgroundResource(getResources().getDrawable(R.drawable.main_img2));
还可以从xml中获取对布局的引用,如下所示

relativelayout = (RelativeLayout)findViewById(R.id.your_layout);

id未定义为xml文件谢谢你的好意谢谢你的好意,@dipali你的好意,我今晚睡觉SetBackgroundResource接受一个int,不能设置为null谢谢你的好意。@Balvinder Singh!你的好意,我今晚睡觉