setBackgroundDrawable()已弃用
所以我的sdk从15到21,当我调用setBackgroundDrawable() ,Android Studio告诉我它已经被弃用了。 
我想过用它来绕过它:
 int sdk = android.os.Build.VERSION.SDK_INT; if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) { layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.img_wstat_tstorm)); } else { layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm)); } 
但是,然后,我在“setBackground()”得到一个错误。
那么,你将如何处理呢?
 这是一个有趣的话题。 你做这件事的方式显然是正确的。 这实际上只是一个命名决定的变化。 正如这个答案指出的, setBackground()只是调用setBackgroundDrawable() : 
 public void setBackground(Drawable background) { //noinspection deprecation setBackgroundDrawable(background); } @Deprecated public void setBackgroundDrawable(Drawable background) { ... } 
你可以看到这个线程的更多信息,所有这一切。
也许你可以试试以下内容:
 setBackgroundResource(R.drawable.img_wstat_tstorm); 
这很有趣,因为这个方法已经被弃用了,但是如果你看一下Android源代码,你会发现:
  /** * Set the background to a given Drawable, or remove the background. If the * background has padding, this View's padding is set to the background's * padding. However, when a background is removed, this View's padding isn't * touched. If setting the padding is desired, please use * {@link #setPadding(int, int, int, int)}. * * @param background The Drawable to use as the background, or null to remove the * background */ public void setBackground(Drawable background) { //noinspection deprecation setBackgroundDrawable(background); } 
你得到一个错误,因为getResources()。getDrawable()接受一个id(int)不是一个可绘制的参数。 尝试这个:
 layout.setBackground(getResources().getDrawable(R.id.img_wstat_tstorm)); 
这在我的情况是正确的解决这个问题
  imageView.setBackgroundResource(images[productItem.getPosition()]); 
我正在使用minSdkVersion 16和targetSdkVersion 23以下是我的工作,它使用ContextCompat.getDrawable(context,R.drawable.drawable);
 而不是使用: layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm)); 
而是使用:
 layout.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.img_wstat_tstorm)); 
  getActivity()在一个片段中使用,如果从一个活动调用使用this 
 BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.Nome_imgem)); getSupportActionBar().setBackgroundDrawable(background);