setBackgroundDrawable () deprecated
donc mon sdk va de 15 à 21 et quand j'appelle setBackgroundDrawable()
, Android Studio me dit qu'il est déprécié.
j'ai pensé à le contourner en utilisant:
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));
}
Mais ensuite, j'obtiens une erreur à "setBackground()".
alors, comment vous y prendriez-vous?
9 réponses
C'est un sujet intéressant. La façon dont vous le faites est correcte, apparemment. Il est en fait juste une appellation décision de changement. Comme cette réponse fait remarquer, setBackground()
appelle simplement setBackgroundDrawable()
:
public void setBackground(Drawable background) {
//noinspection deprecation
setBackgroundDrawable(background);
}
@Deprecated
public void setBackgroundDrawable(Drawable background) { ... }
vous pouvez voir ce fil pour plus d'informations sur tout cela.
peut-être que vous pouvez essayer ce qui suit:
setBackgroundResource(R.drawable.img_wstat_tstorm);
c'est drôle parce que cette méthode est dépréciée, mais si vous regardez le code source Android vous trouverez ceci:
/**
* 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);
}
vous obtenez une erreur parce que getResources().getDrawable () prend un id (int) non un drawable comme argument. Essayez ceci:
layout.setBackground(getResources().getDrawable(R.id.img_wstat_tstorm));
Correct au 15 août 2018
utiliser les bibliothèques de soutien
Drawable drawable = ResourcesCompat.getDrawable(getResources(), drawableRes, null);
ViewCompat.setBackground(layout, drawable);
C'est correct dans mon cas Résoudre ce problème
imageView.setBackgroundResource(images[productItem.getPosition()]);
j'utilise une minSdkVersion 16 et targetSdkVersion 23 Ce qui suit fonctionne pour moi, il utilise ContextCompat.getDrawable (context, R. drawable.drawable);
au lieu d'utiliser: layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));
mais Plutôt:
layout.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.img_wstat_tstorm));
getActivity()
est utilisé dans un fragment, si vous appelez à partir d'une activité d'utilisation this
//Java
view.setBackground(ActivityCompat.getDrawable(context, R.drawable.bg))
//Kotlin
view.background = ActivityCompat.getDrawable(context, R.drawable.bg)
BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.Nome_imgem));
getSupportActionBar().setBackgroundDrawable(background);