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?

55
demandé sur MysticMagicϡ 2014-11-26 07:21:08

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.

72
répondu Alex K 2017-05-23 12:10:38

peut-être que vous pouvez essayer ce qui suit:

setBackgroundResource(R.drawable.img_wstat_tstorm);
18
répondu hedgehog 2015-04-20 14:43:29

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);
    }
16
répondu Joaquin Iurchuk 2015-02-04 05:16:17

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));

3
répondu David C Adams 2014-11-26 04:32:48

Correct au 15 août 2018

utiliser les bibliothèques de soutien

Drawable drawable = ResourcesCompat.getDrawable(getResources(), drawableRes, null);
ViewCompat.setBackground(layout, drawable);
3
répondu Stuart Campbell 2018-08-15 11:01:38

C'est correct dans mon cas Résoudre ce problème

 imageView.setBackgroundResource(images[productItem.getPosition()]);
2
répondu Sonu Kumar 2016-04-12 11:45:04

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

0
répondu Vostro 2016-03-30 10:28:05
//Java
view.setBackground(ActivityCompat.getDrawable(context, R.drawable.bg))

//Kotlin 
view.background = ActivityCompat.getDrawable(context, R.drawable.bg)
0
répondu EliaszKubala 2018-03-29 09:19:22
BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.Nome_imgem));
        getSupportActionBar().setBackgroundDrawable(background);
-1
répondu Vando Sep 2017-10-08 04:22:50