web-dev-qa-db-ja.com

Android:RelativeLayoutの背景画像を設定しますか?

実行時にイメージをダウンロードしました。ここで、それをRelativeLayoutの背景として設定します。それは可能ですか?

15
Vivek

Drawableクラスの setBackgroundDrawable 、または多分 createFromPath をチェックしてください。

   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);
22
SteD

代わりに以下を使用してください:

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

これは、R.drawable.newImageは整数を指します。だからあなたはすることができます:

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

Xamarin.Android(クロスプラットフォーム)でこれを試してください-

RelativeLayout relativeLayout = new RelativeLayout (this);

OR

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

そして

relativeLayout.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.imageName));
2
Nitin V. Patil

onCreate関数内:

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

画像の読み込み方法:

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

オープンメソッド:

> 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
0
The Demz