Andere Tipps

Verwenden Sie stattdessen:

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

Dies funktioniert, weil R.drawable.newImage auf eine Ganzzahl verweist.Sie könnten also Folgendes tun:

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

Versuchen Sie dies für Xamarin.Android (plattformübergreifend) -

RelativeLayout relativeLayout = new RelativeLayout (this);

ODER

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

UND

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

In der onCreate-Funktion:

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

Die Methode zum Laden von Bildern:

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

Die offene Methode:

> 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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top