Ho scaricato l'immagine in fase di esecuzione. Ora voglio impostarlo come sfondo per il layout relativo come farlo? È possibile?Impostazione dell'immagine di sfondo per il layout relativo?
15
A
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);
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
- 1. Impostazione dell'immagine di sfondo per QPushButton
- 2. Come impostare il valore alfa per il mio layout Relativo?
- 3. Layout relativo ignorato setMargin()
- 4. Layout relativo in ScrollView
- 5. OnTouchListener non funziona con il layout relativo
- 6. Catturare l'istantanea del layout Relativo con sfondo trasparente dà una bitmap con sfondo nero non trasparente
- 7. Android "layout_alignParentBottom" nel layout relativo
- 8. Impostazione del colore di sfondo per SKLabelNode?
- 9. Android - Impostazione di sfondo personalizzabile per ClickSpan
- 10. Layout relativo. Tra 2 articoli
- 11. iOS8 Auto layout programmaticamente pin al margine di layout relativo
- 12. Ridimensiona l'immagine di sfondo per avvolgere il contenuto del layout
- 13. Differenza tra il frame e il layout relativo?
- 14. Layout sfondo Android alpha
- 15. Impostare l'immagine di sfondo su Layout relativo utilizzando Glide in Android
- 16. Perché il mio layout relativo occupare larghezza schermo intero
- 17. Come correggere gli angoli arrotondati impostati in Android per il layout Relativo o altro?
- 18. MENO Sfondo CSS con percorso relativo
- 19. Perché scegliere il layout Vincoli poiché abbiamo già il layout relativo?
- 20. Ottieni il colore di sfondo di un layout
- 21. Modifica layout Android da Relativo a Linear
- 22. layout relativo dipendenze circolari Android errore
- 23. Impostazione di un'immagine di sfondo Emacs
- 24. Impostazione AppWidget sfondo per un widget colpisce anche gli altri
- 25. Questo layout RelativeLayout o il relativo genitore LinearLayout è inutile
- 26. come cambiare il colore del bordo del layout relativo?
- 27. Impostazione di un'immagine di sfondo per una barra delle schede
- 28. Impostazione colore sfondo cella fullcalendar
- 29. Impostazione div sfondo utilizzando Trianglify
- 30. Impostazione android: id per componente di layout lineare
Come detto non ho immagine. Lo sto scaricando, ciò significa che non è presente nella cartella drawable. è? – Vivek
Sì, è possibile utilizzare createFromPath per creare un oggetto Drawable dal percorso in cui risiederà l'immagine appena scaricata in – SteD
Il metodo 'setBackgroundDrawable (Drawable)' dal tipo ** View ** è ** deprecato ** ... – Confuse