2011-02-05 13 views

Odpowiedz

21

Zapoznaj się z setBackgroundDrawable, lub może createFromPath w klasie Drawable.

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

Jak powiedziałem, że nie mam obrazu. Pobierany jest to, co oznacza, że ​​nie występuje w folderze do rysowania. to jest? – Vivek

+0

Tak, można użyć createFromPath do utworzenia obiektu Drawable ze ścieżki, w której znajduje się nowo pobrany obraz w – SteD

+1

Metoda 'setBackgroundDrawable (Drawable)' z typu ** Widok ** jest ** przestarzała ** ... – Confuse

3

Zamiast używać:

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

To działa, ponieważ R.drawable.newImage odnosi się do liczby całkowitej. Więc można zrobić:

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

Spróbuj na Xamarin.Android (Cross Platform) -

RelativeLayout relativeLayout = new RelativeLayout (this); 

LUB

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

I

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

W onCreate funkcja:

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

Sposób obraz ładowania:

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

Otwarta metoda:

> 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 
Powiązane problemy