setBackgroundDrawable () deprecato


89

Quindi il mio SDK passa da 15 a 21 e quando chiamo setBackgroundDrawable()Android Studio mi dice che è deprecato.

Ho pensato di girarci intorno usando:

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

Ma poi, ottengo un errore in "setBackground ()".

Quindi, come lo affronteresti?


Ricevi un errore o un avviso?
— Bryan Herbst

che valore hai della versione min sdk nel manifest?
— Manmohan Badaya,

4
usa setbackgroundresource (R.drawable.img_wstat_tstorm); per la versione superiore. setBackgroundDrawable è deprecato nella versione superiore, questa speranza ti aiuta
— prakash

L'SDK minimo è 15. Ho "setBackground ()" sottolineato in rosso ma l'app funziona quindi immagino sia un avviso
— Makoto

Devi ricevere Add @SupressWarning
— SweetWisher ツ

Risposte:


105

È un argomento interessante. Il modo in cui lo stai facendo è corretto, a quanto pare. In realtà è solo una modifica della decisione di denominazione. Come sottolinea questa risposta , setBackground()chiama solo setBackgroundDrawable():

public void setBackground(Drawable background) {
    //noinspection deprecation
    setBackgroundDrawable(background);
}

@Deprecated
public void setBackgroundDrawable(Drawable background) { ... }

Puoi vedere questo thread per ulteriori informazioni su tutto questo.


20
Dovresti notare che setBackground()non funzionerà per la versione precedente all'API16, un'alternativa potrebbe esseresetBackgroundResource
— Mood

26

forse puoi provare quanto segue:

setBackgroundResource(R.drawable.img_wstat_tstorm);

18

È divertente perché quel metodo è deprecato, ma se guardi il codice sorgente di Android troverai questo:

   /**
     * 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);
    }

12

Corretto al 15 agosto 2018

Usa le librerie di supporto

Drawable drawable = ResourcesCompat.getDrawable(getResources(), drawableRes, null);
ViewCompat.setBackground(layout, drawable);

8

Stai ricevendo un errore perché getResources (). GetDrawable () accetta un id (int) non un disegnabile come argomento. Prova questo:

layout.setBackground(getResources().getDrawable(R.id.img_wstat_tstorm));


setBackground prevede solo l'ID Drawable
— SweetWisher ツ

Non sei corretto. Dai documenti dell'API: android.view.View.setBackground (sfondo disegnabile); Parametri: background Il Drawable da usare come sfondo o null per rimuovere lo sfondo.
— David C Adams,

5

Usa questo:

myView.background = ContextCompat.getDrawable(context, R.id.my_drawable)

3
//Java
view.setBackground(ActivityCompat.getDrawable(context, R.drawable.bg))

//Kotlin 
view.background = ActivityCompat.getDrawable(context, R.drawable.bg)

2

Questo è corretto nel mio caso Risolvi questo problema

 imageView.setBackgroundResource(images[productItem.getPosition()]);

1

Corretto al 23 novembre 2018

Kotlin:

view.background = resources.getDrawable(R.drawable.ic_image,theme)

Se includi il parametro Theme.


0

Sto usando un minSdkVersion 16 e targetSdkVersion 23 Quanto segue funziona per me, usa ContextCompat.getDrawable (context, R.drawable.drawable);

Invece di usare: layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));

Piuttosto usa:

layout.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.img_wstat_tstorm));

getActivity() viene utilizzato in un frammento, se si chiama da un'attività use this


Domanda per minSdk 15
— Harish Gyanani

-1
BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.Nome_imgem));
        getSupportActionBar().setBackgroundDrawable(background);

Sarebbe davvero utile se
— volessi
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.