2011-02-05 8 views

risposta

21

Controllare lo setBackgroundDrawable, o forse createFromPath nella classe Disegnabile.

RelativeLayout rLayout = (RelativeLayout) findViewById (R.id.rLayout); 
    Resources res = getResources(); //resource handle 
    Drawable drawable = res.getDrawable(R.drawable.newImage); //new Image that was added to the res folder 

    rLayout.setBackground(drawable); 
+0

Come detto non ho immagine. Lo sto scaricando, ciò significa che non è presente nella cartella drawable. è? – Vivek

+0

Sì, è possibile utilizzare createFromPath per creare un oggetto Drawable dal percorso in cui risiederà l'immagine appena scaricata in – SteD

+1

Il metodo 'setBackgroundDrawable (Drawable)' dal tipo ** View ** è ** deprecato ** ... – Confuse

3

utilizzare invece:

View lay = (View) findViewById(R.id.rLayout); 
lay.setBackgroundResource(R.drawable.newImage); 

Questo funziona perché R.drawable.newImage si riferisce ad un numero intero. Così si potrebbe fare:

int pic = R.drawable.newImage; 
lay.setBackgroundResource(pic); 
2

Prova questo per Xamarin.Android (Cross Platform) -

RelativeLayout relativeLayout = new RelativeLayout (this); 

O

RelativeLayout relativeLayout = (RelativeLayout)FindViewById (Resource.Id.relativeLayout); 

E

relativeLayout.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.imageName)); 
0

Nel onCreate funzione:

RelativeLayout baseLayout = (RelativeLayout) this.findViewById(R.id.the_layout_id); 

Drawable drawable = loadImageFromAsset(); 

if(drawable != null){ 
    baseLayout.setBackground(drawable); 
    Log.d("TheActivity", "Setting the background"); 
} 

Procedimento immagine caricamento:

public Drawable loadImageFromAsset() { 

    Drawable drawable; 

    // load image 
    try { 

     // get input stream 
     InputStream ims = getAssets().open("images/test.9.png"); 

     //Note: Images can be in hierarical 

     // load image as Drawable 
     drawable = Drawable.createFromStream(ims, null); 

    } 
    catch(IOException ex) { 
     Log.d("LoadingImage", "Error reading the image"); 
     return null; 
    } 

    return drawable; 
} 

Il metodo aperto:

> public final InputStream open (String fileName, int accessMode) 
> 
> Added in API level 1 Open an asset using an explicit access mode, 
> returning an InputStream to read its contents. This provides access to 
> files that have been bundled with an application as assets -- that is, 
> files placed in to the "assets" directory. 
> 
> fileName --- The name of the asset to open. This name can be hierarchical. 
> 
> accessMode --- Desired access mode for retrieving the data. 
> 
> Throws IOException 
Problemi correlati